具有关联类型的协议通用功能的协议 [英] Protocol with associatedtype Protocol for Generic functions

查看:100
本文介绍了具有关联类型的协议通用功能的协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于具有关联类型的协议通用功能的协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆