I need to define a protocol which can be called in a class that use some Objective-c type
But doing that doesn't work:
enum NewsCellActionType: Int { case Vote = 0 case Comments case Time } @objc protocol NewsCellDelegate { func newsCellDidSelectButton(cell: NewsCell, actionType: NewsCellActionType) }
You get he error
Swift enums cannot be represented in Objective-C
If I don't put the @objc tag on my protocol it'll crash the app as soon as it's called in a class which adopt the protocol AND inherit from an Objective-C type class (like a UIViewController).
So my question is, how should I declare and pass my enum with the @objc tag?
That's where the @objc attribute comes in: when you apply it to a class or method it instructs Swift to make those things available to Objective-C as well as Swift code.
Yes, enums can conform protocols. You can use Swift's own protocols or custom protocols. By using protocols with Enums you can add more capabilities.
In Swift language, we have Structs, Enum and Classes. Struct and Enum are passed by copy but Classes are passed by reference. Only Classes support inheritance, Enum and Struct don't.
Apple just announced today that Swift 1.2 (included with xcode 6.3) will support exposing enums to objective-c
https://developer.apple.com/swift/blog/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With