2

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?

cajsaiko
  • 1,017
  • 1
  • 8
  • 15

1 Answers1

2

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];
Jack Lawrence
  • 10,664
  • 1
  • 47
  • 61
  • So the IDE inserts the type casting for me? if viewWithTag returns a UILabel it also has no warning, it is also possible to get a wrong type, but I think the most important part is not whether there has explicit type casting, which type you think it is is more important, and I can write less code with __kindof, thanks for your answer! – cajsaiko Nov 24 '15 at 04:20
  • The compiler accepts the code without the type cast. This is really where it comes down to Objective-C vs Swift–Objective-C takes a looser (more unsafe) approach, whereas Swift is stricter (safer) and requires an explicit cast always. – Jack Lawrence Nov 24 '15 at 06:48