tags:

views:

337

answers:

1

I have a few subclasses of UITableViewCell from the 2.0 days that override the deprecated designated initializer:

- (id)initWithFrame:(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?

+1  A: 

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.

Kevin Ballard
I think you're one to something there. I forgot about alloc returning (id) and the compiler not knowing for sure. I bet if I change the subclasses to just override the new DI, then the uses by classes will now be flagged because of the deprecation on UITableViewCell. Time to go try that.
Patrick Burleson
Well, I just changed all the subclasses to override initWithStyle:reuseIdentifer: and all uses of initWithFrame:reuseIdentifer: are still not flagged. Although other elements of UITableViewCell are (i.e. setText:). Not that it's completely accurate, but when I right click initWithFrame:reuseIdentifier: and choose "Go to Definition", I'm taken straight to UITableViewCell's header which has deprecation marks. Before my change, I got all my subclasses as choices too.
Patrick Burleson
Xcode's "Go to Definition" is relying on the indexer, which isn't the same thing as the compiler. Try casting the result of +alloc to your cell subclass. The reason why -setText: would definitely work is because you're calling it on a variable of the appropriate type. Similarly the following should work:`MyTableViewCell *cell = [MyTableViewCell alloc]; [cell initWithFrame:frame reuseIdentifier:ident];`
Kevin Ballard
You're right, if I cast to (UITableViewCell *) on the results to +alloc, I get the warning. That means single line alloc+init's never get deprecation warnings. Bummer. Being sure I've changed this in all locations will be based on Find In Project results. Sadness.
Patrick Burleson