ios – Swift 3:有没有办法将对象同时转换为类和协议?

ios – Swift 3:有没有办法将对象同时转换为类和协议?,第1张

概述我已经阅读了Apple的 Swift iBook(Type Casting& Protocols)的相关章节,但我似乎找到了一种方法来指定一个对象是符合特定协议的特定类的实例. 作为tableView(_:,cellForRowAt :)中的一个例子,我想将tableView.dequeueReusableCell返回的单元格(withIdentifier:reuseID,for:indexPat 我已经阅读了Apple的 Swift iBook(Type Casting& Protocols)的相关章节,但我似乎找到了一种方法来指定一个对象是符合特定协议的特定类的实例.

作为tableVIEw(_:,cellForRowAt :)中的一个例子,我想将tableVIEw.dequeueReusableCell返回的单元格(withIDentifIEr:reuseID,for:indexPath)转换为符合RLMEntityCapableCell协议的UItableVIEwCell的子类(只是指定了conformers)有一个名为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

所以显然也不是正确的方法.

我更愿意指出,为了符合协议,对象必须从UItableVIEwCell或UICollectionVIEwCell继承,但协议的基础只能限于类类型而不能进一步.

编辑:

这里的想法是为Realm对象提供一个通用数据源,它可以像Array和Dictionary那样利用泛型.每个表视图中使用的单元格将特定于要显示的实体,但数据源只知道该单元格将是符合RLMEntityCapableCell的UItableVIEwCell的子类.所有数据源需要担心的是告诉单元格需要显示哪个实例(它始终是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协议添加超类要求(如您最初所希望的那样).我们可能需要等待Swift 5 总结

以上是内存溢出为你收集整理的ios – Swift 3:有没有办法将对象同时转换为类和协议?全部内容,希望文章能够帮你解决ios – Swift 3:有没有办法将对象同时转换为类和协议?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1105108.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存