Using @ is available with saved properties.

I have an application that uses local notifications and supports iOS 10. I'm trying to add support for iOS 9, which requires me to use the old location notification API. I am trying to use @available and #available in my iOS 10 code, and I cannot figure out how to get my central variable for iOS 10 devices only.

When I set my goal from iOS 10 to 9, I get an error for this variable: "UNUserNotificationCenter is only available on iOS 10.0 or later." He suggests adding “@available (iOS 10.0, *)” to the whole class that I don’t want to do, as this class has code that will be used for iOS 9. I appreciate any suggestions on how to limit my center for iOS 10.

class ViewController: UIViewController, UITextFieldDelegate {

  let center = UNUserNotificationCenter.current()
  ...
+4
source share
3 answers

@available can be used around an entire class or one or more functions, but not for properties.

UNUserNotificationCenter, current , , center UNUserNotificationCenter.current(), center ?

+1

( ).

, Any, , ( ).

private var _selectionFeedbackGenerator: Any? = nil
@available(iOS 10.0, *)
fileprivate var selectionFeedbackGenerator: UISelectionFeedbackGenerator {
    if _selectionFeedbackGenerator == nil {
        _selectionFeedbackGenerator = UISelectionFeedbackGenerator()
    }
    return _selectionFeedbackGenerator as! UISelectionFeedbackGenerator
}
+10
let validClass = NSClassFromString("UNUserNotificationCenter") != nil

validClass iOS 10, :

if validClass
   // iOS 10 code
else
   // Earlier than iOS 10 code
-1

Source: https://habr.com/ru/post/1668062/


All Articles