__deprecated__ not triggering compiler warning with subclass
I have a few subclasses of UITableViewCell from the 2.0 days that override the deprecated designated initializer:
- (id)initWithFr开发者_开发百科ame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
This code does some additional setup for the cell and I'm converting the classes to use the new designated intializer:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
What's odd is that only place the compiler throws a warning about the method being deprecated is in the subclasses themselves when they call
[super initWithStyle:style reuseIdentifier:reuseIdentifier]
All the classes that use these subclasses don't get the warning. To try and force it, I added the following to the subclasses' headers:
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier __attribute__ ((deprecated));
Oddly enough, now all deprecation warnings have gone away.
One thing I haven't tried is to convert the subclasses to override the new designated initializer and see if the places in the code that use these cells now get the deprecation warning from the super class.
I still find it odd that after I added the deprecated attribute, all warnings went away.
Any ideas why that would be?
I believe the problem is because +alloc
returns an object of type id
, so the compiler isn't certain that your -initWithFrame:reuseIdentifier:
is actually the same method as the one that's deprecated. However, when you call [super initWithFrame:reuseIdentifier:]
the compiler does know for a fact that the method is deprecated. If you try something like [(MyTableCellClass *)[MyTableCellClass alloc] initWithFrame:frame reuseIdentifier:ident]
I expect that would warn.
Though this doesn't really explain why all warnings go away when you redeclare the method yourself.
精彩评论