xcode - 如何在不使用 gestalt 的情况下获取 MAC 10.10 中的操作系统版本字符串

标签 xcode macos operating-system osx-yosemite osx-yosemite-beta

我的目标是收集 MAC 10.10 的操作系统版本、发布详细信息。

我找到了一种方法来收集 MAC 10.10 以及以前的版本详细信息。链接中的版本

How do I determine the OS version at runtime in OS X or iOS (without using Gestalt)?

当我编译它时,它在“objc_msgSend_stret”处抛出错误,当我搜索/usr/include/objc 文件夹本身在我的 MAC 中不存在时。我只能看到 gcc 存在,并且我的所有代码都是用它构建的。

有没有最好的方法来复制输出

[[NSProcessInfo processInfo], @selector(operatingSystemVersion)] 到结构体“MyOperatingSystemVersion”?

typedef struct {
        NSInteger majorVersion;
        NSInteger minorVersion;
        NSInteger patchVersion;
} MyOperatingSystemVersion;

if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) {
    MyOperatingSystemVersion version = ((MyOperatingSystemVersion(*)(id, SEL))objc_msgSend_stret)([NSProcessInfo processInfo], @selector(operatingSystemVersion));
    // do whatever you want with the version struct here
}
else {
    UInt32 systemVersion = 0;
    OSStatus err = Gestalt(gestaltSystemVersion, (SInt32 *) &systemVersion);
    // do whatever you want with the systemVersion as before
}

最佳答案

我猜您不是使用 10.10 SDK 构建的,否则您可以简单地删除对 objc_msgSend_stret 的调用并直接调用operatingSystemVersion 。

// Built with 10.10 SDK
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) {
    NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
    // do whatever you want with the version struct here
}
else {
    UInt32 systemVersion = 0;
    OSStatus err = Gestalt(gestaltSystemVersion, (SInt32 *) &systemVersion);
    // do whatever you want with the systemVersion as before
}

如果您使用的是早期 SDK,则 NSOperatingSystemVersion-[NSProcessInfo OperatingSystemVersion] 均未定义。由于您无法在不出现编译器错误的情况下使用 -[NSProcessInfo OperatingSystemVersion],因此调用它并获取非对象类型的返回值的更安全方法是使用 NSInitation。这在技术上仍然存在风险,因为您将返回值分配给不同类型的结构(MyOperatingSystemVersionNSOperatingSystemVersion),但您已将它们定义为相同的,因此它应该没问题。我用 Xcode 5/10.9 SDK 对此进行了测试,效果很好。

// Built with 10.9 or earlier SDKs
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)]) {
    MyOperatingSystemVersion version;
    NSMethodSignature *methodSignature = [[NSProcessInfo processInfo] methodSignatureForSelector:@selector(operatingSystemVersion)];
    NSInvocation *inv = [NSInvocation invocationWithMethodSignature:methodSignature];
    inv.selector = @selector(operatingSystemVersion);
    [inv invokeWithTarget:[NSProcessInfo processInfo]];
    [inv getReturnValue:&version];
    // do whatever you want with the version struct here
}
else {
    UInt32 systemVersion = 0;
    OSStatus err = Gestalt(gestaltSystemVersion, (SInt32 *) &systemVersion);
    // do whatever you want with the systemVersion as before
}

这应该可以解决问题,但最安全的做法是使用 10.10 SDK 进行构建并使用第一个示例。

关于xcode - 如何在不使用 gestalt 的情况下获取 MAC 10.10 中的操作系统版本字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25430059/

相关文章:

xcode - 从 github 克隆 repo 到 xcode 问题

c++ - Qt 菜单栏不显示

iphone - 在 Mac 上从 iPhone 获取实时数据

linux - Linux 中的文件访问方法

linux - X个线程是通过X个进程实现的吗?

java - java.io.File 中的空字符是否对存在检查有效?

ios - 如何在 iOS 8 应用程序中制作交互式通知?

ios - Xcode IB Storyboard方向和容器 View

swift - Xcode Swift 根据屏幕尺寸移动 UIElements

objective-c - NSClosableWindowMask 对话框行为异常