Check protocol compliance when a protocol contains an enumeration in Swift?

In Swift, to check protocol compliance with is or as? downcasting, you must mark the protocol with the @objc attribute. Once you mark a protocol with this attribute, it seems that you cannot have a protocol with enum as a property, because enumerations 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 will receive an error message: error: property cannot be marked @objc because its type cannot be represented in Objective-C

Here is a complete example: http://swiftstub.com/475659213/

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

+6
source share
1 answer

This is not an answer, but I noticed a compilation error in your "quick stub", Human should be defined as follows:

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

You tried to create an instance of enum from a string literal.

I'm a little surprised that checking protocol compliance requires @obj - it's just ugly!

0
source

Source: https://habr.com/ru/post/975320/


All Articles