如何知道应用程序运行在什么Mac OS? [英] How to know what Mac OS the app is running on?

查看:178
本文介绍了如何知道应用程序运行在什么Mac OS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某些项目中看到过:

I've seen in some projects something like:

#if .....
    code...
#endif

但我现在找不到... >
让我们说,例如,如果应用程序运行在10.8应用程序做1事情,如果不是应用程序做其他事情。
什么代码检查它是否在10.8上运行?

谢谢。

but i can't find it now...
Let's say, for example, if the app is running on 10.8 the app does 1 thing, if not the app does other thing. Whats to code to check if it's running on 10.8?
Thanks.

推荐答案

您可能提出了错误的问题。除非在极少数情况下,您不应该关心用户正在运行的系统版本。

You're probably asking the wrong question. Except in very rare cases, you should not care what system version the user is running. Instead, you should be checking if the specific thing you're interested in is available.

例如,如果苹果在Mac OS X 10.9中引入了一个MagicHologram类,你想要要使用,您不检查用户是否正在运行Mac OS X 10.9。相反,你检查MagicHologram类是否可用。如果是,您可以使用它。如果没有,它不可用。甚至没有为什么。也许他们正在运行10.8。但也许五年后,苹果决定完全删除MagicHologram类。

For instance, if Apple introduces a MagicHologram class in Mac OS X 10.9 that you want to use, you don't check if the user is running Mac OS X 10.9. Instead, you check if the MagicHologram class is available. If it is, you can use it. If not, it's not available. It doesn't even matter why. Maybe they're running 10.8. But maybe it's five years later, and Apple's decided to drop the MagicHologram class entirely.

(另外,请记住,你需要弱链接到HologramKit, )

(Also, keep in mind that you'd need to weak link to HologramKit, the library that provides the MagicHologram class.)

同样,如果他们向NSString引入了一个新方法,而不是检查操作系统版本,你可以检查NSString是否知道新的方法。

Likewise, if they introduce a new method to NSString, instead of checking the OS version you'd check if NSString knows about the new method.

也就是说,NSApplication.h包含一个外部常量 NSAppKitVersionNumber 。你可以比较这个常量,如 NSAppKitVersionNumber10_7 这(应该注意)是像1138,而不是10.7的数字。这里只有几个地方是合适的,主要是类是私有的和未公开的,但在记录并成为SDK的公共部分的一部分之前有重大变化。

That said, NSApplication.h includes an external constant called NSAppKitVersionNumber. You can compare this to constants like NSAppKitVersionNumber10_7 which (it should be noted) are numbers like 1138, not 10.7. There's only a few places this is appropriate, mostly where classes were private and undocumented but got major changes before being documented and becoming a part of the public parts of the SDK. Also, it might be helpful if you want to avoid a specific bug that's been fixed since.

总结:


  1. 检测个别类和方法,应该覆盖99.44%的案例。

  2. 使用 NSAppKitVersionNumber c> NSAppKitVersionNumber10_7 ,以涵盖类或方法检测对您的情况。

  3. 前两个点覆盖所有正常案例。你不应该走了。但如果你必须有基于人性化版本的行为,看看abarnert的答案如下。

  4. 不要使用 operatingSystemVersionString ,这是特别列出为不安全的解析。 li>
  1. Detect individual classes and methods, which should cover 99.44% of your cases.
  2. Use NSAppKitVersionNumber and NSAppKitVersionNumber10_7 to cover those cases where class or method detection would lie to you.
  3. Those first two points cover all normal cases. You should go no further. But if you must have behaviour based on humane version, look at abarnert's answer below. It's the sanest way to get them.
  4. Don't use operatingSystemVersionString, which is specifically listed as not safe for parsing.

参考资料/更多信息:


  • SDK兼容性指南如果您希望您的应用程序定位iOS或Mac OS X的特定版本或多个版本。

    • 使用基于SDK的开发描述如何使用弱链接类,方法和函数来支持在多个版本的操作系统上运行。

    • SDK Compatibility Guide "Read this document if you want your application to target a specific version or multiple versions of iOS or Mac OS X."
      • Using SDK-Based Development Describes how to use weakly linked classes, methods, and functions to support running on multiple versions of an operating system.

      这篇关于如何知道应用程序运行在什么Mac OS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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