I am trying to write a function that takes an object and a type as arguments and returns a boolean indicating whether the object is of this type. There seems to be no type type, so I'm not sure how to do this. The best I could do is
func objectIsType<T>(object: AnyObject, someObjectOfType: T) -> Bool { return object is T }
So, I can do objectIsType(x, 5) to check if x Int or objectIsType(x, "hi") to see if it is a string, but I would like to call objectIsType(x, Int) to see t22> is Int and objectIsType(x, String) to see if it is a String . Is something like this possible?
Edit:
The speed of Airspeed Velocity improved my function and made a big conclusion that it is doing what is already doing. New Feature:
func objectIsType<T>(object: Any, someObjectOfType: T.Type) -> Bool { return object is T }
What I'm trying to do is check that the values โโof the [String: Any] dictionary are of the type that I expect. For instance:
let validator: [String: Any.Type] = [ "gimme an int": Int.self, "this better be a string": String.self ] let validatee: [String: Any] = [ "gimme an int": 3, "this better be a string": "it is!" ] for (key, type) in validator { if !objectIsType(validatee[key], type) { selfDestruct() } }
But I get an error, <>protocol.Type is not convertible to T.type . I looked at the Metatype documentation, but I'm still a bit confused.
source share