I have this enum in Swift
enum Direction: Int{
case Left2Right = 0, Right2Left
}
And this protocol
@objc protocol CellDelegate : NSObjectProtocol{
optional func has(direction:SwipeDirection) -> Bool
}
I am getting this error
Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C
Can anybody tell me why do i get this error and how to fix it please? Thanks!
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.
Finally, let's take a look at how enum cases relate to functions, and how Swift 5.3 brings a new feature that lets us combine protocols with enums in brand new ways. A really interesting aspect of enum cases with associated values is that they can actually be used directly as functions.
Swift protocols on their side do not allow optional methods. But if you are making an app for macOS, iOS, tvOS or watchOS you can add the @objc keyword at the beginning of the implementation of your protocol and add @objc follow by optional keyword before each methods you want to be optional.
To define Optional Protocol in swift you should use @objc keyword before Protocol declaration and attribute / method declaration inside that protocol.
The @objc attribute makes the protocol compatible (i.e. usable) with Objective C. But swift enums (prior to 1.2 beta) are not available in Objective C. So you cannot use a swift enum in that protocol.
I think that the best solution is to use swift 1.2 - it's still in beta (as of today), but it's possible to apply the @objc
attribute to swift enums.
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