ios - swift 3 : Is there a way to cast an object to a class and protocol at the same time?

标签 ios swift swift3 swift-protocols existential-type

我已经通读了 Apple 的 Swift iBook(类型转换和协议(protocol))的相关部分,但我似乎可以找到一种方法来指定对象是符合特定协议(protocol)的特定类的实例。

作为 tableView(_: , cellForRowAt: ) 中的示例,我想转换 tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath) 返回的单元格作为 UITableViewCell 的子类,它符合 RLMEntityCapableCell 协议(protocol)(只是指定符合者有一个名为 item 的变量,它是 的一个实例>Object,或其子类之一)。

这条路线可行,但双重施法似乎过多:

protocol RLMEntityCapableCell: class  {
     var item: Object { get set }
}

 public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath) as! RLMEntityCapableCell // Cast here so we can set item
    cell.item = items[indexPath.row]
    return cell as! UITableViewCell // Cast again so the return type is right…
}

另一种方法:

var cell = tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath) 
           as! RLMEntityCapableCell, UITableViewCell

给出这个错误:

type annotation missing in pattern

显然这也不是正确的方法。

我更愿意指定为了符合协议(protocol),对象必须从 UITableViewCellUICollectionViewCell 继承,但协议(protocol)的基础只能是有限的到类类型,不再进一步。

编辑:

这里的想法是为 Realm 对象提供一个通用数据源,该数据源像 ArrayDictionary 一样利用泛型。每个 TableView 中使用的单元格将特定于要显示的实体,但数据源只知道该单元格将是 UITableViewCell 的子类,符合 RLMEntityCapableCell .所有数据源需要担心的是告诉单元格它需要显示什么实例(它总是 Object 的子类),单元格将从那里获取它并根据需要配置自己。

最佳答案

不,这不可能......

下一个 Swift 版本(第 4 版)可能会带来您正在寻找的东西,一个名为 Class and Subtype Existentials 的新功能。 :

This proposal brings more expressive power to the type system by allowing Swift to represent existentials of classes and subtypes which conform to protocols.

The proposal keeps the existing & syntax but allows one of the elements to be either AnyObject or of class type (e.g., SomeClass & SomeProtocol).

然后你可以说:

var cell = tableView.dequeueReusableCell(withIdentifier: reuseID, for: indexPath) 
           as! UITableViewCell & RLMEntityCapableCell

但是,当然,您将无法使用它向您的 RLMEntityCapableCell 协议(protocol)添加父类(super class)要求(正如您最初希望的那样)。我们可能需要等待 Swift 5:)


其他一些使用上述类和子类型存在 (Swift 4) 特性的例子:

protocol P {}
struct S {}
class C {}
class D : P {}
class E : C, P {}

let u: S & P // Compiler error: S is not of class type
let v: C & P = D() // Compiler error: D is not a subtype of C
let w: C & P = E() // Compiles successfully

和:

protocol P {}
class C {}
class D : C { }
class E : C { }
class F : D, P { }

let t: C & D & P = F() // Okay: F is a subclass of D and conforms to P
let u: D & P = t       // Okay: D & P is equivalent to C & D & P
let v: C & D & P = u   // Okay: C & D & P is equivalent to D & P
let w: D & E & P       // Compiler error: D is not a subclass of E or vice-versa

关于ios - swift 3 : Is there a way to cast an object to a class and protocol at the same time?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43686763/

相关文章:

ios - 重新启动 GameViewController/GameScene

swift - 检查函数是否被调用?

ios - WKInterfaceTable向上滚动刷新

swift - 遍历 NSDictionary 中的字符串数组

ios - Coredata 推断映射模型在我的旧数据库上失败

ios - 如何在 iOS 中使用 VoIP

ios - 不确定如何设置 View 对象并连接 IBOutlet 代码

ios - 滚动表格 View 并单击搜索器崩溃应用程序

ios - swift 3 iOS 兼容性

ios - 如何分离这两个功能?