Search code examples
iosxcodeviewwithtag

Why there is no warning while casting with viewWithTag?


For example,

UIImageView *iconView = [cell.contentView viewWithTag:TestTag];

I remember that if I don't cast the type explicitly there will has warning before, but now Xcode doesn't show that, and my Xcode version is 7.1.1, is that a new feature or I modify some configs? who can tell me why?


Solution

  • There's a new (Xcode 7) keyword in Objective-C, __kindof, that allows you to better express the return value of a method. Instead of viewWithTag: returning UIView *, it can return __kindof UIView * which tells the compiler something like:

    Accept any implicit downcast of the return type if the type is a UIView or a subclass of UIView.

    Since UIImageView is a subclass of UIView, no explicit cast is necessary. On the other hand, the following line of code will cause a compiler error because NSDate is not a subclass of UIView:

    NSDate *date = [cell.contentView viewWithTag:TestTag];