0

I've had reports, confirmed by me, that one of the features in one of my apps breaks under 10.7.3. Upon investigation, it seems that 10.7.3 introduced a private -stringForKey: method on NSDictionary.

[NSDictionary respondsToSelector:@selector(stringForKey:)] returns NO, as one would expect.

But I have a category method on NSDictionary to implement a -stringForKey: method, as follows (so it can be used for NSNumber and NSDate values too). Under 10.7.2 and earlier, it works fine; under 10.7.3, it returns nil. Getting the object and description directly works fine. So it must be a category conflict.

- (NSString *)stringForKey:(id)key;
{
    return [[self objectForKey:key] description];
}

I guess this is another argument in favor of prefixing category methods, despite advice I got from an Apple Application Frameworks Evangelist.

Can others confirm this? I don't think it's a third-party app conflicting; I think it's a change in 10.7.3.

Community
  • 1
  • 1
Dejal
  • 813
  • 6
  • 18

1 Answers1

2

You should always prefix category methods you create on framework classes. No question about it. It doesn't matter whether or not 10.7.3 introduced this method, the fact that you're declaring it without a prefix is wrong.

Incidentally, testing [NSDictionary respondsToSelector:@selector(stringForKey:)] isn't necessarily going to work. NSDictionary is a class cluster, so you're just asking the abstract superclass, whereas private methods may only exist on concrete subclasses.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • Well, since Apple has conflicting views on prefixing (see my linked post), it's not quite so clear-cut. – Dejal Feb 10 '12 at 23:52
  • 1
    @Dejal: "Apple" is not a monolithic entity that has one opinion about everything. Just look at the mixture of styles in various sample code. Since you didn't post the actual response you got from the evangelist, I can't tell whether your interpretation of what he said is correct, but it doesn't really matter. The simple fact is, you should *always* prefix category methods you declare on framework classes. There's no reason not to, and as you've found out, there's a *big* reason to do so. – Lily Ballard Feb 11 '12 at 00:01
  • @Dejal Kevin is correct. I have posted an answer at [the question you posted a link to in your question here](http://stackoverflow.com/a/9241380/927947) to help you deal with that issue. – NJones Feb 11 '12 at 15:32
  • Followed up in that previous question. – Dejal Feb 11 '12 at 18:22