How do I do weak linking in Swift?

后端 未结 2 1522
太阳男子
太阳男子 2020-12-03 01:54

In Objective-C, if I wanted to use a specific class that\'s only present in a new version of iOS, I would do something like this:

if( [UIBlurEffect class] )          


        
2条回答
  •  再見小時候
    2020-12-03 02:20

    Just to add my two cents since I ended up using this to my immediate dismay upon distributing the app to our testers. There is a bug in the Swift compiler (Xcode <= 6.1.1) whereby when building in Release mode the compiler actually doesn't return nil when calling NSClassFromString.

    To check me simply change your configuration to Release and see how she crashes.

    I ended up having to explicitly check for iOS versions or a simple respondsToSelector call in other place.

    So where my code looked like this:

        if NSClassFromString("UIVisualEffectView") != nil {
            //use the blur safely
        } else {
            //abandon blur! try something safer
        }
    

    I ended up having to use this instead

    switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
            case .OrderedSame, .OrderedDescending:
                //go wild with blur
            case .OrderedAscending:
                //blur shall not pass here!
            }
    

    This question dealt with the same issue - UIAlertView is crashing app on iOS 7

提交回复
热议问题