简体   繁体   中英

Using @available with stored properties

I have an app that uses local notifications and supports iOS 10. I am trying to add iOS 9 support which requires me to use the old location notification API. I am trying to use @available and #available on my iOS 10 code and I can't figure out how to get my center variable to only be for devices running iOS 10.

When I set my target from iOS 10 to 9 I get the error message for this variable:

UNUserNotificationCenter is only available on iOS 10.0 or newer.

It suggests I add @available(iOS 10.0, *) to my entire class which I don't want to do since there is code in this class that will be used for iOS 9. I appreciate any suggestions on how to limit my center property to just iOS 10.

class ViewController: UIViewController, UITextFieldDelegate {
  
  let center = UNUserNotificationCenter.current()
  ...
}

Here is one potential solution (thanks toblog post ). The idea is to use a stored property with a type of Any and then create a computed property that will cast the stored property (and instantiate it if necessary).

private var _selectionFeedbackGenerator: Any? = nil
@available(iOS 10.0, *)
fileprivate var selectionFeedbackGenerator: UISelectionFeedbackGenerator {
    if _selectionFeedbackGenerator == nil {
        _selectionFeedbackGenerator = UISelectionFeedbackGenerator()
    }
    return _selectionFeedbackGenerator as! UISelectionFeedbackGenerator
}

Another option is to use lazy (however, this makes the variable read-write):

@available(iOS 10.0, *)
private(set) lazy var center = UNUserNotificationCenter.current()

Update as of Xcode 14.0.0 beta 1

Unfortunately it seems that lazy properties are now considered stored properties, so this workaround no longer works. Writing your own computed var with backing storage is the best solution for now.

Edit: It turns out that this approach was an error all along, it just didn't produce a diagnostic. That was fixed with Swift 5.7: https://github.com/apple/swift/pull/41112


Previous answer

I know this is an older question but I wanted to add an answer for people who come here via Google as I did.

As kgaidis and Cœur mentioned, you can use @available on computed properties. However, lazy variables are considered computed properties and so you can use @available on them too. This has the nice benefit of removing the boilerplate of the extra stored property and the forced casts - in fact, it leaves no evidence of the property in your pre-iOS 10 code.

You can simply declare it like this:

@available(iOS 10.0, *)
private(set) lazy var center = UNUserNotificationCenter.current()

Unfortunately there's no way to make it completely read-only but the private(set) at least makes it read-only outside of the class.

@available could be used around a whole class or one or more functions, but not for properties.

Regarding your UNUserNotificationCenter usage, current returns a singleton that never changes, so why not just remove the center constant, and just use UNUserNotificationCenter.current() where center is used?

Similar idea than kgaidis, by using a separate stored property of a type accepted in any version. But Any may be too generic, as it cannot be declared weak for instance, so you may want to replace it with a conforming protocol in some situations:

private weak var _notificationCenterDelegate: NSObjectProtocol?
@available(iOS 10.0, *)
var notificationCenterDelegate: UNUserNotificationCenterDelegate? {
    return _notificationCenterDelegate as? UNUserNotificationCenterDelegate
}

The code I use for feedback generators in my apps which supports iOS 9. As you can see, it's simple and it has no force casts. The main idea is to store value in Any? property and use it via computed one.

private var storedFeedbackGenerator: Any? = nil
@available(iOS 10.0, *)
private var feedbackGenerator: UISelectionFeedbackGenerator {
    if let generator = storedFeedbackGenerator as? UISelectionFeedbackGenerator {
        return generator
    }

    let generator = UISelectionFeedbackGenerator()
    generator.prepare()
    storedFeedbackGenerator = generator
    return generator
}

对于目标-c

@property (nonatomic, strong) CustomClass *object API_AVAILABLE(ios(10.0));

I have this error in Flutter project in XCode 14. One of the external packages were using:

@available(iOS 14.0, *)

My current fix is:

Update version in Podfile platform:ios, '14.0'

Reset pods cd ios && rm -rf Pods/ Podfile.lock && pod install --repo-update

Issue: https://github.com/pichillilorenzo/flutter_inappwebview/issues/1216

Starting from @kgaidis solution, I did this for apple sign in button:

private var _appleButton: UIControl? = nil
@available(iOS 13.0, *)
private var appleButton: ASAuthorizationAppleIDButton {
    if _appleButton == nil {
        let btn = ASAuthorizationAppleIDButton()
        btn.addTarget(self, action: #selector(handleAppleSignInPress), for: .touchUpInside)
        _appleButton = btn
    }
    return _appleButton as! ASAuthorizationAppleIDButton
}

This way I still use appleButton with @available(iOS 13.0, *)

if #available(iOS 13.0, *) {
    holderView.addSubview(appleButton)
}

I faced this issue when updated to xCode 14.*

pod update
pod install 

worked for me

Stored properties cannot be marked potentially unavailable with '@available

Just change NSPersistentCloudKitContainer to NSPersistentContainer and check its working fine or not

let validClass = NSClassFromString("UNUserNotificationCenter") != nil

Use validClass to decide code specific for iOS 10 like :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM