This line of code used to work with Swift 2, but now is incorrect in Swift 3.
if gestureRecognizer.isMember(of: UITapGestureRecognizer) { }
I get this error: Expected member name or constructor call after type name.
What is the correct way to use isMember(of:)
?
“Use the type check operator (is) to check whether an instance is of a certain subclass type. The type check operator returns true if the instance is of that subclass type and false if it is not.” Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.
In Swift, there are two kinds of types: named types and compound types. A named type is a type that can be given a particular name when it's defined. Named types include classes, structures, enumerations, and protocols. For example, instances of a user-defined class named MyClass have the type MyClass .
Swift is also an object-oriented programming language. And, like other oop languages, it also supports the concept of objects and classes. An object is simply a collection of data (variables) and methods (functions). Similarly, a class is a blueprint for that object.
The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.
Most likely, you'll want to not only check the type, but also cast to that type. In this case, use:
if let gestureRecognizer as? UITapGestureRecognizer { } else { /* not a UITapGestureRecognizer */ }
These operators are only available in Swift, but still work when dealing with Objective C types.
as
operatorThe
as
operator performs a cast when it is known at compile time that the cast always succeeds, such as upcasting or bridging. Upcasting lets you use an expression as an instance of its type’s supertype, without using an intermediate variable.
as?
operatorThe
as?
operator performs a conditional cast of the expression to the specified type. Theas?
operator returns an optional of the specified type. At runtime, if the cast succeeds, the value of expression is wrapped in an optional and returned; otherwise, the value returned isnil
. If casting to the specified type is guaranteed to fail or is guaranteed to succeed, a compile-time error is raised.
This is the second most preferable operator to use. Use it to safely handle the case in which a casting operator can't be performed.
as!
operatorThe
as!
operator performs a forced cast of the expression to the specified type. Theas!
operator returns a value of the specified type, not an optional type. If the cast fails, a runtime error is raised. The behavior ofx as! T
is the same as the behavior of(x as? T)!
.
This is the least preferable operator to use. I strongly advise against abusing it. Attempting to cast an expression to an incompatible type crashes your program.
If you merely want to check the type of an expression, without casting to that type, then you can use these approaches. They are only available in Swift, but still work when dealing with Objective C types.
is
operatoris
operator checks at runtime whether the expression can be cast to the specified type. It returns true
if the expression can be cast to the specified type; otherwise, it returns false
isKind(of:)
type(of:)
is
operator, this can be used to check the exact type, without consideration for subclasses.type(of: instance) == DesiredType.self
isMember(of:)
These are all methods on NSObjectProtocol
. They can be used in Swift code, but they only apply work with classes that derive from NSObjectProtocol
(such as subclasses of NSObject
). I advise against using these, but I mention them here for completeness
isKind(of:)
is
operator instead.isMember(of:)
type(of: instance) == DesiredType.self
instead.conforms(to:)
is
operator instead.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With