我可以在我的类中存储符合 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/