iiFreeman
iiFreeman

Reputation: 5175

Swift: enum type as a property

Can I store enum type (or class) conforms to protocol to var inside my class?

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

Getting compile error:

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

cause I getting no compile error here, but here is no single word about protocol:

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

achieved it by putting

var sourceType : InformationServiceItemProtocol.Type = InformationServiceMenuItem.self

no compile errors here, but I can't use it, anywhere I'm trying to access sourceType I'm getting compilation failed with

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

for example:

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
}

Upvotes: 3

Views: 1179

Answers (1)

Sulthan
Sulthan

Reputation: 130092

You have to distinguish between types and metatypes:

var sourceType: InformationServiceItemProtocol.Protocol = InformationServiceMenuItem.self

EDIT to answer edits:

That's a compiler bug. Report it to Apple.

Upvotes: 1

Related Questions