Mac OS X 10.7.3 添加 -[NSDictionary stringForKey:] 方法?
我收到过经我确认的报告,我的一个应用程序中的一项功能在 10.7.3 下出现故障。经过调查,10.7.3 似乎在 NSDictionary
上引入了一个私有的 -stringForKey:
方法。
[NSDictionary respondsToSelector:@selector(stringForKey:)]
返回 NO,正如人们所期望的那样。
但我在 NSDictionary
上有一个类别方法来实现 -stringForKey:
方法,如下所示(因此它可以用于 NSNumber
和 NSDate
值也是如此)。在 10.7.2 及更早版本下,它工作正常;在 10.7.3 下,它返回 nil。直接获取对象和描述效果很好。所以这一定是类别冲突。
- (NSString *)stringForKey:(id)key;
{
return [[self objectForKey:key] description];
}
我想这是支持为类别方法添加前缀的另一个论点,尽管 我从 Apple 应用程序框架传播者那里得到的建议。
其他人可以证实这一点吗?我不认为这是第三方应用程序的冲突;我认为这是 10.7.3 中的变化。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该始终为您在框架类上创建的类别方法添加前缀。毫无疑问。不管 10.7.3 是否引入了这个方法,你声明它时不带前缀都是错误的。
顺便说一句,测试
[NSDictionary respondsToSelector:@selector(stringForKey:)]
不一定有效。 NSDictionary 是一个类簇,因此您只需询问抽象超类,而私有方法可能只存在于具体子类上。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.