【问题标题】:Is there any way to detect supported options in SDK有什么方法可以检测 SDK 中支持的选项
【发布时间】:2012-11-03 00:44:20
【问题描述】:

使用基于 SDK 的开发”解释了如何使用弱链接类、方法和函数...

我用过这个例如

if ([NSByteCountFormatter class]) {
    ...
}

有没有办法检测支持的选项,例如

NSRegularExpressionSearch
The search string is treated as an ICU-compatible regular expression.
If set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch.
You can use this option only with the rangeOfString:... methods and stringByReplacingOccurrencesOfString:withString:options:range:.

Available in OS X v10.7 and later.

【问题讨论】:

    标签: cocoa osx-mountain-lion


    【解决方案1】:

    用于测试一个类,例如NSUserNotificationCenter,存在,做

    if(NSClassFromString(@"NSUserNotificationCenter"))
    {
        //...
    }
    

    用于测试是否为常数,例如NSWindowDidChangeBackingPropertiesNotification,存在,做

    BOOL NSWindowDidChangeBackingPropertiesNotificationIsAvailable = (&NSWindowDidChangeBackingPropertiesNotification != NULL);
    if (NSWindowDidChangeBackingPropertiesNotificationIsAvailable) 
    {
        //...
    }
    

    也请查看此答案:Check if constant is defined at runtime in Obj-C

    在你的情况下,这看起来像

    BOOL NSRegularExpressionSearchIsAvailable = (&NSRegularExpressionSearch != NULL);
    if (NSRegularExpressionSearchIsAvailable)
    {
        //...
    }
    

    【讨论】:

    • 这可能适用于定义为 APPKIT_EXTERN NSString * const NSWindowDidExitVersionBrowserNotification 的 NSWindowDidChangeBackingPropertiesNotification,因此您可以获取其地址。 NSRegularExpressionSearch 在 NSString.h 中定义为 NSRegularExpressionSearch NS_ENUM_AVAILABLE(10_7, 3_2) = 1024 所以你不能取它的地址,会在编译时被转换。
    猜你喜欢
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    相关资源
    最近更新 更多