@available - make仅在OS X上可用

时间:2020-12-25 16:54:34

Is it possible to mark a function/computed variable as available on OS X and unavailable on iOS (and vice versa)? Something such as @available(OSX 10.0, iOS unavailable).

是否有可能将OS X上可用的函数/计算变量标记为iOS(反之亦然)?诸如@available(OSX 10.0,iOS不可用)之类的东西。

I know about the #if OSX statement, however, I'm trying to achieve something that Apple has done for the NSURLThumbnailKey - it's marked as NS_AVAILABLE_MAC(10_10); in ObjC and the compiler will complain that the symbol is unavailable rather than that the compiler cannot resolve the symbol.

我知道#if OSX语句,但是,我正在努力实现Apple为NSURLThumbnailKey所做的事情 - 它被标记为NS_AVAILABLE_MAC(10_10);在ObjC中,编译器会抱怨符号不可用,而不是编译器无法解析符号。

Also, I've found that the #if statements in Swift are buggy, for example, defining func myFunc(_, arg1:) and func myFunc(_, somethingElse:) within the #if scope will result in an error saying that myFunc has already been defined. Which is why I'm trying to avoid using #if.

另外,我发现Swift中的#if语句有问题,例如,在#if范围内定义func myFunc(_,arg1 :)和func myFunc(_,somethingElse :)将导致错误说明myFunc已经定义了。这就是我试图避免使用#if的原因。

1 个解决方案

#1


1  

You should be able to do something like:

你应该能够做到这样的事情:

@available(OS X 10.10, *) 

To force it for OS X 10.10 higher only, or:

要强制它仅适用于OS X 10.10,或者:

if #available(OS X 10.10, *)

There's some more information available on Hacking With Swift.

有关Hacking With Swift的更多信息。

Multiple specification:

@available(iOS, unavailable, message="nope")
@available(OS X 10.10, *)
func someFunc()

#1


1  

You should be able to do something like:

你应该能够做到这样的事情:

@available(OS X 10.10, *) 

To force it for OS X 10.10 higher only, or:

要强制它仅适用于OS X 10.10,或者:

if #available(OS X 10.10, *)

There's some more information available on Hacking With Swift.

有关Hacking With Swift的更多信息。

Multiple specification:

@available(iOS, unavailable, message="nope")
@available(OS X 10.10, *)
func someFunc()