6

In Swift, in order to check protocol conformance with is or as? downcasting you must mark the protocol with the @objc attribute. Once you mark a protocol with that attribute it seems you can not have a protocol with an enum as a property because enums cannot be represented in Objective-C.

enum Language:String {
    case English = "English"
    case Spanish = "Spanish"
    case German = "German"
}

@objc protocol Humanizable {
    var language:Language { get set }
}

You'll get an error: error: property cannot be marked @objc because its type cannot be represented in Objective-C

Here is full example: http://swiftstub.com/475659213/

In the example if you change the Language to String then it works fine.

Johnston
  • 20,196
  • 18
  • 72
  • 121
  • Thanks for the swiftstub.com link, very useful! An interesting problem too. –  Sep 15 '14 at 19:21
  • @Graff No prob. At least you can try it out and see the issue I am facing. – Johnston Sep 15 '14 at 19:53
  • Yep, I see it. I fooled around a bit but haven't come up with anything yet. Hopefully there's a good answer out there, the most I could come up with was using an Int or String in place of the enum. –  Sep 15 '14 at 20:22
  • possible duplicate of [How to pass swift enum with @objc tag](http://stackoverflow.com/questions/24140545/how-to-pass-swift-enum-with-objc-tag) – Gordon Tucker Apr 16 '15 at 21:56

1 Answers1

0

This is not an answer, but I did spot a compile error in your 'swift stub', Human should be defined as follows:

class Human:Humanizable {
  var name:String = "Frank"
  var language:Language = .English
}

You were trying to create an enum instance from a string literal.

I am a little surprised that protocol conformance checking requires @obj - that's just ugly!

ColinE
  • 68,894
  • 15
  • 164
  • 232
  • Yah it's weird: “You can check for protocol conformance only if your protocol is marked with the @objc attribute” Straight from the iBook. – Johnston Sep 15 '14 at 22:46