Swift:枚举类型作为属性

时间:2014-07-08 14:11:59

标签: xcode enums swift protocols

我可以将枚举类型(或类)符合我的类中的var协议吗?

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

获取编译错误:

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

因为我在这里没有编译错误,但这里没有关于协议的单词:

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
}

1 个答案:

答案 0 :(得分:1)

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

var sourceType: InformationServiceItemProtocol.Protocol = InformationServiceMenuItem.self

编辑回答编辑:

这是编译器错误。向Apple报告。