为什么Objective-C没有API可用性检查? [英] Why does objective-c not have API availability checking?

查看:168
本文介绍了为什么Objective-C没有API可用性检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift 2具有API 可用性检查.

Swift 2 have API availability checking.

使用对您来说太新的API时,编译器会给您一个错误 最低目标操作系统

The compiler will give you an error when using an API too new for your minimum target OS

objective-c编译器为什么不能做同样的事情?

Why can't the objective-c compiler do the equivalent?

我用谷歌搜索了目标c API的可用性检查,结果只有2个,所以我认为目标c的编译器无法做到这一点.

I googled objective c API availability checking and only swift 2 results came out so I assume the compiler for objective c can't do that.

推荐答案

警告(Swift使其成为错误)多年以来都没有在Clang编译器中实现,但它不是Object-C固有的限制(尽管由于其动态性质,您将无法捕获所有情况),也无法使用Swift术语.

The warning (Swift makes it an error) just hadn't been implemented in the Clang compiler for years, but it's not an inherent Objective-C limitation (although due to its dynamic nature, you won't be able to catch all cases), nor Swift terminology.

使用可用信息注释标头的Apple宏(例如NS_CLASS_AVAILABLE)和源属性(__attribute__((visibility(...)))__attribute__((availability(...))))已经存在了很多年,它们在Apple的SDK中得到了广泛使用.宏是在FoundationNSObjCRuntime.hAvailability.h/AvailabilityMacros.h系统头文件中定义的,编译器可以(并且确实)读取它们.

The Apple macros (e.g., NS_CLASS_AVAILABLE) and source attributes (__attribute__((visibility(...))), __attribute__((availability(...)))) to annotate headers with availability information have been there for years, and they are widely-used in Apple's SDKs. The macros are defined in Foundation's NSObjCRuntime.h, and the Availability.h/AvailabilityMacros.h system headers, and the compiler can (and does) read them.

2015年初,在Clang的-Wpartial-availability警告已添加 master分支,但是这种提交/警告直到(包括)Xcode 7.2才进入Apple的Clang版本.在Xcode 7.2中将警告标志添加到项目时,您将获得unknown warning option日志,但是该标志在Xcode 7.3中可用.目前尚无预定义设置,但是您可以在 Build Settings 下将标记添加到Other Warning Flags.

In early 2015, the -Wpartial-availability warning has been added to Clang's master branch, but this commit/warning hadn't made its way into Apple's version of Clang until (including) Xcode 7.2. You will get an unknown warning option log when adding the warning flag to a project in Xcode 7.2, but the flag is available in Xcode 7.3. There's currently no predefined setting for it, but you can add the flag to Other Warning Flags under Build Settings.

还有其他一些使用LLVM库来检测部分可用的API的工具,例如 Deploymate .在我的毕业论文中,我开发了一种工具,该工具直接集成到Xcode中,并且基于对Clang编译器的修改.代码仍然是在线,但是我没有跟上通用的Clang开发的步伐,所以除了用于学习目的之外,它没有多大用处.但是,官方"代码(上面已链接)更加简洁.

There are other tools that use LLVM libraries to detect partially available APIs, e.g., Deploymate. For my diploma thesis, I developed a tool that integrates directly into Xcode and is based on a modification to the Clang compiler. The code is still online, but I haven't kept up with the general Clang development so it won't be of much use, except for learning purposes. However, the "official" code (linked above) is much cleaner and better.

编辑:从Xcode 9开始,可用性检查也适用于Objective-C(和C). -Wunguarded-availabilityif (@available(iOS 11, *)) {...}代替使用上述警告标志(它不支持临时/本地提高部署目标并因此导致大量误报),而是检查并提高以下目标块的部署目标:代码.它默认情况下处于关闭状态,但-Wunguarded-availability-new默认情况下处于打开状态,并开始检查iOS/tvOS 11,watchOS 4和High Sierra之外的任何内容.有关详细信息,请参见 Xcode 9 Beta发行说明.需要使用开发者帐户登录.

Starting with Xcode 9, availability checking will work for Objective-C (and C), too. Instead of using the above-mentioned warning flag, which does not support raising the deployment target temporarily/locally and therefore causes plenty of false positives, there's -Wunguarded-availability, and if (@available(iOS 11, *)) {...} to check and raise the deployment target for the following block of code. It is off by default, but -Wunguarded-availability-new will be on by default, and starts checking anything beyond iOS/tvOS 11, watchOS 4, and High Sierra. More details on that can be found in the Xcode 9 beta release notes, which currently requires signing in with a developer account.

这篇关于为什么Objective-C没有API可用性检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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