Rawan
Rawan

Reputation: 1639

nil value returned by NSClassFromString swift 2.0

I'm using the following code in my project to draw fading on a view:

let customBlurClass: AnyObject.Type = NSClassFromString("_UICustomBlurEffect")!
let customBlurObject: NSObject.Type = customBlurClass as! NSObject.Type
self.blurEffect = customBlurObject.init() as! UIBlurEffect
self.blurEffect.setValue(1.0, forKeyPath: "scale")
self.blurEffect.setValue(radius, forKeyPath: "blurRadius")
super.init(effect: radius == 0 ? nil : self.blurEffect)

sometimes on Fabric I get crash report from the app on this line:

let customBlurClass: AnyObject.Type = NSClassFromString("_UICustomBlurEffect")!

which means that the NSClassFromString return nil value,

I searched a lot about this problem but no useful answers,

Please Help,

Thanks.

Upvotes: 0

Views: 275

Answers (1)

Dave Weston
Dave Weston

Reputation: 6635

The most likely explanation is that those crashes occur on devices running iOS 8 or earlier. _UICustomBlurEffect was introduced in iOS 9.

You should do:

if let blurClass = NSClassFromString("_UICustomBlurEffect") {
    // set up blur view
}

to avoid crashes on devices where it's not supported.

Upvotes: 2

Related Questions