6
votes

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.

1
Thanks for the swiftstub.com link, very useful! An interesting problem too. - user887210
@Graff No prob. At least you can try it out and see the issue I am facing. - Johnston
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. - user887210

1 Answers

0
votes

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!