如何有条件地使用新的Cocoa API [英] How to conditionally use a new Cocoa API

查看:291
本文介绍了如何有条件地使用新的Cocoa API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在10.6 Apple添加了 + [NSPropertyListSerialization dataWithPropertyList:format:options:error:] 并标记了旧的 + [NSPropertyListSerialization dataFromPropertyList:format:errorDescription :] 作为已过时,很快就被弃用。在10.6及以上版本上使用较新的调用,并仍然在较早的操作系统版本上运行的一种方法是这样的:

In 10.6 Apple added +[NSPropertyListSerialization dataWithPropertyList:format:options:error:] and labeled the older +[NSPropertyListSerialization dataFromPropertyList:format:errorDescription:] as obsolete and soon to be deprecated. One way to use the newer call on 10.6 and above, and still run on earlier OS releases, would be something like this:

if ([NSPropertyListSerialization respondsToSelector:@selector(dataWithPropertyList:format:options:error:)]) {
    data = [NSPropertyListSerialization dataWithPropertyList:dict
                                                      format:NSPropertyListXMLFormat_v1_0
                                                     options:0
                                                       error:&err];
} else {
    data = [NSPropertyListSerialization dataFromPropertyList:dict
                                                      format:NSPropertyListXMLFormat_v1_0
                                            errorDescription:&errorDescription];        
}

针对10.4 SDK(与该版本兼容) :警告:'NSPropertyListSerialization'可能不响应'+ dataWithPropertyList:format:options:error:'更糟糕的是,由于编译器不知道这个选择器,

Built against the 10.4 SDK (for compatibility with that release), this results in: warning: 'NSPropertyListSerialization' may not respond to '+dataWithPropertyList:format:options:error:' And, worse, since the compiler does not know about this selector, it may pass the arguments incorrectly.

NSInvocation是否已被批准/调用新API的最佳方法,就SDK而言,还不存在?

Is NSInvocation the approved/best way to call new APIs that, as far as the SDK is concerned, don't yet exist?

推荐答案

另一种做事方式是将缺少的方法自己声明为类的类别。这将使编译器停止抱怨没有找到该方法,当然,您仍然需要运行时检查您已经在做,以避免实际调用该方法。你可能还想使用可用性宏来包装这样一个声明,所以一旦你使用10.5 / 10.6 SDK,它将被忽略,你不会得到一个不同的编译器投诉。看起来像这样:

One other way of doing things is to declare the missing method yourself as a category of the class in question. This will get the compiler to stop complaining about not finding the method, though of course you'll still need the runtime check you're already doing to avoid actually calling the method. You might also want to wrap such a declaration using availability macros, so that it will be ignored once you do move up to using the 10.5/10.6 SDK and you won't get a different compiler complaint down the line. That would look something like this:

#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_4 //ignore when compiling with the 10.5 SDK or higher
@interface NSPropertyListSerialization(MissingMethods)

+ (NSData *)dataWithPropertyList:(id)plist format:(NSPropertyListFormat)format options:(NSPropertyListWriteOptions)opt error:(NSError **)error;

@end
#endif

这篇关于如何有条件地使用新的Cocoa API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆