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

发布于 2024-12-01 04:31:30 字数 573 浏览 0 评论 0原文

我确信这真的很简单,但我什至不确定如何搜索它,因为我已经看到了我认为所谓的编译器标志的示例?,但一般来说,可可中有条件地运行特定的最佳方法是什么方法在支持它的操作系统版本上。例如,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];

I am sure this is really simple, but I'm not sure even how to search for it, as I have seen example of what I think are called compiler flags?, but in general whats the best method in cocoa of condtionally running a specific method on Operating System Versions that support it. as an example the NSDateFormatter Class has the setDoesRelativeDateFormatting method which only works on 10.6 (on the Mac) and higher.

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吹梦到西洲 2024-12-08 04:31:30

以下是 Adium 如何检测到它正在 Snow Leopard 上运行,或者更好地执行类似的操作(这是 NSApplication 上的一个类别):

//Make sure the version number defines exist; when compiling in 10.5, NSAppKitVersionNumber10_5 isn't defined 
#ifndef NSAppKitVersionNumber10_5
#define NSAppKitVersionNumber10_5 949
#endif 

- (BOOL)isOnSnowLeopardOrBetter
{
    return (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5);
}

然后您所要做的就是

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

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

Here's how Adium detected that it was running on Snow Leopard or better to do stuff like this (this was in a category on NSApplication):

//Make sure the version number defines exist; when compiling in 10.5, NSAppKitVersionNumber10_5 isn't defined 
#ifndef NSAppKitVersionNumber10_5
#define NSAppKitVersionNumber10_5 949
#endif 

- (BOOL)isOnSnowLeopardOrBetter
{
    return (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5);
}

Then all you have to do is

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

You can find the version numbers for AppKit by command-clicking on NSAppKitVersionNumber to jump to its definition.

沧笙踏歌 2024-12-08 04:31:30

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

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 协议的 -respondsToSelector: 了解更多信息。

For this particular case, it should be easy enough to do the following:

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];
}

See NSObject protocol's -respondsToSelector: for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文