具有关联类型的协议通用功能的协议

编程入门 行业动态 更新时间:2024-10-25 04:24:27
本文介绍了具有关联类型的协议通用功能的协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在其他协议的通用功能中提供确认协议? 我试图使它像这样工作,但是那是不可能的,或者我犯了一些错误.

Is it possible to provide confirming protocol in generic functions of another protocol? I tried make it work like this, but it's impossible, or I made some mistakes.

我的代码:

protocol DataModelProtocol { associatedtype ObjectProtocol: Protocol func fetchObjects<T: ObjectProtocol>() -> [T]? func fetch<T: ObjectProtocol>(object: T) -> T? func delete<T: ObjectProtocol>(allObjectOf type: T.Type) func insert<T: ObjectProtocol>(_ object: T) func save<T: ObjectProtocol>(_ object: T) func update<T: ObjectProtocol>(_ object: T) func delete<T: ObjectProtocol>(_ object: T) }

错误消息:

从非协议,非类类型'Self.ObjectProtocol'继承

inheritance from non-protocol, non-class type 'Self.ObjectProtocol'

Xcode错误图片

它只能像这样工作,但我想使其更加灵活:

It works only like this, but I want to make it more flexible:

protocol DataModelProtocol { typealias ObjectProtocol = NSManagedObject ... }

推荐答案

如果您将返回类型的责任交给对象类本身,这也许会更容易.

This would perhaps be easier is you gave responsibility of the return types to the object classes themselves.

您将需要两个协议,但要避免混合使用协议和泛型:

You would need two protocols but it would avoid mixing protocols and generics:

// The first protocol is for the base class of data objects protocol DataProtocol {} // The protocol provides the "typed" equivalents of the model's // data manipulation methods. // By using an extension to DataProtocol, this only needs to // be done once for all models and data objects. extension DataProtocol { static func fetchObjects(from model:DataModelProtocol) -> [Self]? { return model.fetchObjects(object: Self.self) as! [Self]? } static func fetch(from model:DataModelProtocol) -> Self? { return model.fetch(object: Self.self) as! Self? } // ... } // The second protocol is for the data models // It requires implementation of the data manipulation methods // using the general "DataProtocol" rather than any specific class // The actual instances it produces must be of the appropriate class // however because they will be type casted by the DataProtocol's // default methods protocol DataModelProtocol { func fetchObjects(object:DataProtocol.Type) -> [DataProtocol]? func fetch(object:DataProtocol.Type) -> DataProtocol? // ... and so on }

... 这是如何使用协议的简单(简单)示例. (我有意选择不使用核心数据来说明解决方案的一般性) ...

... Here is a simple (naive) example of how the protocols can be used. (I intentionally chose not to use core data to illustrate the generality of the solution) ...

// The base class (or each one) can be assigned the DataProtocol // (it doesn't add any requirement) class LibraryObject : DataProtocol {} class Author: LibraryObject { var name = "" } class Book: LibraryObject { var title = "" } // This is a simple class that implements a DataModelProtocol // in a naive (and non-core-data way) struct LibraryModel:DataModelProtocol { var authors:[Author] = [ Author(), Author() ] var books:[Book] = [ Book(), Book(), Book(), Book(), Book() ] func fetchObjects(object: DataProtocol.Type) -> [DataProtocol]? { return object == Book.self ? books : object == Author.self ? authors : nil } func fetch(object:DataProtocol.Type) -> DataProtocol? { return nil } }

... 使用协议与您的方法有所不同,因为您将从对象类开始,而不是将它们作为参数传递给模型 ...

... Using the protocols will be a bit different from your approach because you would be starting from the object classes rather than passing them as parameter to the model ...

var library = LibraryModel() let allBooks = Book.fetchObjects(from:library) // this almost reads like english

更多推荐

具有关联类型的协议通用功能的协议

本文发布于:2023-10-28 08:35:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536115.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:协议   类型   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!