Checking object type?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Checking object type?

Alex Hall
Hello everyone,
I have a method that needs to check what kind of recognizer a UIGestureRecognizer is. In looking up how to do that, I have found that Swift appears to lack any kind of type comparison or member checking. I found Type and dynamicType, but those don't appear to be usable in comparisons. For instance, this won't work:

if gestureRecognizer.metaType==UITapGestureRecognizer.metaType

I tried mixing Type in there as well, but that fails too. There must be a way to do this that I'm overlooking, but I'm not sure what it is. Any suggestions would be great. Thanks.

--
Have a great day,
Alex Hall

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/7C832D30-E297-4AD2-998D-37A7F8A3DDDC%40icloud.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Checking object type?

Jens Alfke

On Jun 25, 2015, at 7:17 AM, Alex Hall <[hidden email]> wrote:

I have a method that needs to check what kind of recognizer a UIGestureRecognizer is. In looking up how to do that, I have found that Swift appears to lack any kind of type comparison or member checking. I found Type and dynamicType, but those don't appear to be usable in comparisons.

You use the “is” operator to check whether an instance is of a subclass type:

if gestureRecognizer is UITapGestureRecognizer {
}

You can also use "gestureRecognizer as? UITapGestureRecognizer” to check and type-cast in one operation.

This is covered in the “Type Casting” chapter of the book.

—Jens

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/851F34A6-BAE6-4FB1-9360-3C5D33B0E026%40mooseyard.com.
For more options, visit https://groups.google.com/d/optout.