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] ) {
// do something with UIBlurEffect
}
else {
// gracefully fallback to old behavior
}
However, the equivalent Swift:
if UIBlurEffect.self != nil {
let blur: UIBlurEffect = UIBlurEffect(...)
// ...
else {
// ...
}
// also occurs with NSClassFromString("UIBlurEffect")
doesn't have the same functionality.
If run on an environment where NSNewFeature
is available, everything is fine. But if the class isn't defined, I get a link error when starting the application:
dyld: Symbol not found: _OBJC_CLASS_$_UIBlurEffect
So how do I do weak linking in Swift?
Edit Added UIBlurEffect
as specific example.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…