Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 基于操作系统版本的条件方法_Objective C_Cocoa_Nsdateformatter - Fatal编程技术网

Objective c 基于操作系统版本的条件方法

Objective c 基于操作系统版本的条件方法,objective-c,cocoa,nsdateformatter,Objective C,Cocoa,Nsdateformatter,我相信这确实很简单,但我甚至不知道如何搜索它,因为我已经看到了我认为被称为编译器标志的示例,但一般来说,在cocoa中,在支持它的操作系统版本上有条件地运行特定方法的最佳方法是什么。例如,NSDateFormatter类具有setDoesRelativeDateFormatting方法,该方法仅适用于10.6(Mac)及更高版本 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [da

我相信这确实很简单,但我甚至不知道如何搜索它,因为我已经看到了我认为被称为编译器标志的示例,但一般来说,在cocoa中,在支持它的操作系统版本上有条件地运行特定方法的最佳方法是什么。例如,NSDateFormatter类具有setDoesRelativeDateFormatting方法,该方法仅适用于10.6(Mac)及更高版本

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];        
    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

    [dateFormatter setLocale:enLocale];
    [dateFormatter setDoesRelativeDateFormatting:YES];

以下是Adium如何检测到它在雪豹上运行,或者更好地执行类似操作(这属于非应用程序的一个类别):

那么你所要做的就是

if ([NSApp isOnSnowLeopardOrBetter]) { ... }

您可以通过单击NSAppKitVersionNumber命令找到AppKit的版本号,以跳转到其定义。

对于这种特殊情况,应该很容易执行以下操作:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];        
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

[dateFormatter setLocale:enLocale];

if ([dateFormatter respondsToSelector:@selector(setDoesRelativeDateFormatting:)]) {
    [dateFormatter setDoesRelativeDateFormatting:YES];
}

有关更多信息,请参阅
NSObject
协议。

如何防止编译程序基于SDK发出警告?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];        
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *enLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

[dateFormatter setLocale:enLocale];

if ([dateFormatter respondsToSelector:@selector(setDoesRelativeDateFormatting:)]) {
    [dateFormatter setDoesRelativeDateFormatting:YES];
}