Reputation: 2006
I'm trying to add a method on the Any Protocol in swift 2.0, but I get this error, Non-nominal type 'Any' (aka protocol<>) cannot be extended.
Any idea why am I unable to add a protocol extension to the Any type? What could be a possible workaround to this limitation? My intention is to add a getter called isPrimitiveType that returns true if the object is a primitive or is an actual object.
Upvotes: 2
Views: 283
Reputation: 13243
As of Swift 2.1 you cannot extend protocols like Any
and AnyObject
. Probably in future you can do that.
As workaround you can use a generic global free function:
func isPrimitive<T>(value: T) -> Bool {
return value is String || value is Bool || value is Int || value is Float || value is Double
}
Upvotes: 3