简体   繁体   English

是否可以在 Swift 中为特定的 iOS 版本定义一个变量? 在班级

[英]Is it possible to define a variable for only a specific iOS version in Swift? On the class level

Is it possible to achieve this on the Class level of an object?是否可以在对象的类级别上实现这一点? Specifically looking at this to have an app containing WatchConnectivity but also suppporting iOS8专门看这个有一个包含 WatchConnectivity 但也支持 iOS8 的应用程序

class Test {
    if #available(iOS 9, *) {
        let session: WCSession ......
    }
}

You can use a computed static property: 您可以使用计算的static属性:

public class Example {  
  public static var answerToEverything: Int {
    if #available(iOS 9, *) {
      return 42
    } else {
      return 0
    }
  }
}

Or, you can consider using @available attribute instead: 或者,您可以考虑使用@available属性代替:

public class Example {
  @available(iOS, introduced=1.0)
  public static var answerToEverything: Int = 42
}

where 1.0 is the version of your library (not iOS). 其中1.0您的库的版本(不是iOS)。

This would be useful if you're more concerned about making sure it's used on iOS and less concerned about the iOS version. 如果您更关心确保它在iOS上使用而又不关心iOS版本,那么这将很有用。

You can wrap it with a computed property , for instance for UIMenu :您可以使用计算属性包装它,例如UIMenu

@available(iOS 13.0, *)
var menu: UIMenu? { // use this as a getter for what you want
   return _menu as? UIMenu
}
var _menu: Any? // use this reference as a setter for what you want

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 适用于特定iOS版本的Swift扩展 - Swift extension for a specific iOS version 是否可以限制移动应用程序(android / iOS)应用程序仅使用特定的TLS版本? - Is it possible to restrict mobile app (android/iOS) application to use only specific TLS version? 根据iOS版本继承类(swift) - Inherit class depending on iOS version (swift) 是否可以在iOS swift中增加NSNumber变量? - Is it possible to increment a NSNumber variable in iOS swift? iOS Swift Google Maps SDK在特定缩放级别显示标记吗? - iOS Swift Google Maps SDK showing markers at specific zoom level? 如何定义具有多个特定自定义类型的变量? (在斯威夫特中) - How to define a variable with multiple specific custom types ? (in Swift) 仅在 ios swift 2.3 和 swift 3 中更改特定视图控制器的方向 - Change the orientation only for specific view controllers in ios swift 2.3 and swift 3 声明方法不可用,因为 swift 5 中的 iOS 的特定版本 - Declare method unavailable since a specific version of iOS in swift 5 iOS / Swift:是否可以使用 CallKit 阻止特定国家/地区的某些来电? - iOS / Swift: Is it possible to block some incoming calls with CallKit for a specific country? IOS Swift版本中的错误 - Error in IOS Swift Version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM