What is __kindof?

__kindof is a new feature of the Objective-C language announced by Apple, together with other new features such as Nullability Annotations and Generics. Xcode 7 is needed to use __kindof, so be sure to use that when going over through this post.

How to use __kindof in Objective-C?

Suppose we have this property:

The developer reading this code will have an idea that the array must contain UIViews, but it is not guaranteed that it will really contain UIViews, until he reads through all of the code and verifies that indeed only UIViews were inserted to the array. This case can be resolved using Objective-C’s new features, namely the Generics and __kindof.

Using Generics, we can declare the property as the following instead.

Anyone who reads this code will understand the array must contain UIViews. But the catch is, the array must contain only UIViews, but not instances of subclasses of UIViews such as UIWebViews or UIButtons. Trying to insert a non-UIWebView objectc will result to a compiler warning. This problem can be resolved using __kindof.

Using this construct, viewCollection can contain instances of UIViews and its subclasses such as UIWebViews and UIButtons.

Developers should use the new features of Objective-C if given the chance. The new __kindof construct will surely help developers improve their code by being specific with their intents. The Generics construct and __kindof seems strict but I think this is for the good of the developers and the apps that they are making.



Happy Goals for iOS

Happy Goals for iOS

Want an app to help you track and achieve your goals, without the complex stuff, saving you from headaches? Checkout Happy Goals - Habits & Goals Tracker now in the App Store! Happy Goals has been featured in the App Store in over 32 countries. Start now, better late than never!


Thank you!

%d bloggers like this: