Using isKindOfClass with Swift
The proper Swift operator is is
:
if touch.view is UIPickerView {
// touch.view is of type UIPickerView
}
Of course, if you also need to assign the view to a new constant, then the if let ... as? ...
syntax is your boy, as Kevin mentioned. But if you don't need the value and only need to check the type, then you should use the is
operator.
Using isKindOf in Swift
The substitute for isKindOf
in Swift3
is:
if annotation is MKUserLocation {
return nil
}
isKindOfClass method to check Swift ViewController in Objective C
Because of the bug in swift, @Freerunnering answer will not work.
As a work around, we can get the class name as string and then do the string match.
if ([NSStringFromClass([AViewController class] isEqualToString:@"ProjectName.AViewController"]) {
}
Is there a difference between is and isKindOfClass()?
Yes there is a difference: is
works with any class in Swift, whereas isKindOfClass()
works only with those classes that are subclasses of NSObject
or otherwise implement NSObjectProtocol
.
Check isKindOfClass to Dictionary in Swift
This is because isKindOfClass
requires an AnyClass
type – that is, a type that is a class. Dictionaries aren’t classes, they’re structs, and thus can’t be held by an AnyObject
.
If you switch your code to application.isKindOfClass(NSDictionary)
it should work (work in the sense that it will compile, and check if the type is an NSDictionary
).
swift can't call isKindOfClass with String
isKindOfClass()
works only with classes that are subclass of NSObject
.
You can use fieldValue is String
.
if fieldValue is String {
// do something with string
}
Difference between Swift is and isKindOfClass()?
I will assume that the .isKindOfClass()
is the instance method from Cocoa.
- The right side of
is
can be any type or protocol, whereas the argument of.isKindOfClass()
must be a reference type (i.e. class). You can also test for conformance to an@objc
protocol using.conformsToProtocol()
instead of.isKindOfClass()
in the same way. - The left side of
is
is any expression, whereas the receiver of.isKindOfClass()
must be an object reference. The compiler will complain if the compile-time class of the expression is not known to support.isKindOfClass()
, but you can overcome this by casting the left side toAnyObject
. All Swift classes actually support.isKindOfClass()
at runtime. - The right side of
is
is a type that must be hard-coded at compile-time. The argument of.isKindOfClass()
can be a variable or other expression whose value is computed at runtime.
What is the proper way to use if _ is _ or .isKind(of: )
public func isKind(of aClass: AnyClass) -> Bool
This function requires AnyClass
as argument, you must pass a class, using .self
mapView.annotations.forEach {
if !$0.isKind(of: MKUserLocation.self) {
self.mapView.removeAnnotation($0)
}
}
When use is
, you must wrap the expression with parentheses:
mapView.annotations.forEach {
if !($0 is MKUserLocation) {
self.mapView.removeAnnotation($0)
}
}
You are checking boolean value of $0
, not the is
expression, hence the error.
Related Topics
Load View from an External Xib File in Storyboard
How to Dismiss the Keyboard When Editing a Uitextfield
How to Assign an Action for Uiimageview Object in Swift
How to Scroll to the Bottom of a Uitableview on the iPhone Before the View Appears
Xcode - Ld: Library Not Found for -Lpods
Drop-Down List in Uitableview in iOS
Wrong Color in Interface Builder
How to Set the Opacity/Alpha of a Uiimage
Sorting Nsarray of Dictionaries by Value of a Key in the Dictionaries
What Is the Height of Iphone's Onscreen Keyboard
Comparing Nsdates Without Time Component
How to Post String with Special Character and Thai Language Using Xml Parsing in Objective C
How to Change Locale Programmatically with Swift
Use of Unresolved Identifier' in Swift
Foreign Key Relationship Mapping with Restkit
Avfoundation, How to Turn Off the Shutter Sound When Capturestillimageasynchronouslyfromconnection