Clicky

iOS Dev Nugget 134 Checking an Object for its Class in Swift

.

Need to run a code review on your codebase? Hire me

Swift is still very new, but all kinds of interesting libraries are being created around it. Dollar is a great example, providing useful functional programming language helpers. There are 2 parts to the library — Dollar is purely functional and Cent includes extension to built-in classes. The GitHub repository has many examples, but here's a few highlights:

The Swift equivalent for Objective C's isKindOfClass: is the is operator.

You can do:

if obj is UIView {
    //do something
} else {
    //do something else
}

If you are going to use obj, you might as well do:

if let v = obj as? UIView {
    //do something with v
} else {
    //do something else
}

You can use is and as? in the same way to test for protocols.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues