开发者

__deprecated__ not triggering compiler warning with subclass

开发者 https://www.devze.com 2022-12-17 21:40 出处:网络
I have a few subclasses of UITableViewCell from the 2.0 days that override the deprecated designated initializer:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号