xcode - Swift:枚举类型作为属性

标签 xcode enums swift protocols

我可以在我的类中存储符合 var 协议(protocol)的枚举类型(或类)吗?

class MyClass : UIViewController {
    var sourceType : InformationServiceItemProtocol = InformationServiceMenuItem.self
}

获取编译错误:

Type 'InformationServiceMenuItem.Type' Does not conform to protocol 'InformationServiceItemProtocol'

因为我在这里没有遇到编译错误,但是这里没有关于协议(protocol)的单个词:

class MyClass : UIViewController {
    var sourceType = InformationServiceMenuItem.self
}

通过放置实现它

var sourceType : InformationServiceItemProtocol.Type = InformationServiceMenuItem.self

这里没有编译错误,但我无法使用它,在任何我试图访问 sourceType 的地方我都遇到编译失败

1.  While emitting IR SIL function @_TFC12AeroflotIPad28InformationServiceSideMenuVC9tableViewfS0_FTGSQCSo11UITableView_21cellForRowAtIndexPathGSQCSo11NSIndexPath__GSQCSo15UITableViewCell_ for 'tableView' at /Users/PathToProject/InformationServiceSideMenuVC.swift:25:11
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Volumes/Macintosh_Aditional_SSD/AlsoApplications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254

例如:

protocol InformationServiceItemProtocol {
    var title: String { get }
    var subitems: InformationServiceItemProtocol[]? { get }
    class func count () -> Int
}

override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return sourceType.count() // if I'll replace it with 'return 0' no compilation failed message
}

最佳答案

你必须区分类型和元类型:

var sourceType: InformationServiceItemProtocol.Protocol = InformationServiceMenuItem.self

编辑以回答编辑:

那是一个编译器错误。向 Apple 报告。

关于xcode - Swift:枚举类型作为属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24634063/

相关文章:

c# - 存储游戏范围变量的最佳方式

swift - alamofire 没有正确连接到 api

ios - Xcode 堆栈跟踪未出现在控制台中

ios - 转到另一个 Storyboard,但导航 Controller 仍然可见

ios - 一个场景中的相机/图库按钮,另一个场景中的图像

Typescript Map<enum, set<enum>> "No overload matches this call",但我不明白为什么?

java - 什么时候使用不可实例化的类而不是枚举?

json - 如何通过快速更新特定值将数据重写到同一个 json 文件

ios - 测试在 swift 中调用函数的定时器

swift - 在 Xcode 11.0 中滚动时 ImageView 框架没有改变 (11A420a)