Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass swift enum with @objc tag

Tags:

enums

ios

swift

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?

like image 967
Dimillian Avatar asked Jun 10 '14 12:06

Dimillian


People also ask

What does @objc do in Swift?

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.

Can enum conform to Swift protocol?

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.

Can enum be inherited Swift?

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.


1 Answers

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/

enter image description here

like image 82
Oren Avatar answered Sep 23 '22 18:09

Oren