This answer addresses how to use a Swift class from a framework that may not be linked to an app, but is present in the target OS. Relatedly, but a bit different, I'd like to know how to conditionally use a Swift class that may not be included in the target OS. For example, say I have an app, written in Swift, that targets iOS 7. I want to use WKWebView if I'm on iOS 8, and UIWebView if I'm on iOS 7. This doesn't work:
if NSClassFromString("WKWebView") == nil {
var wv = WKWebView()
} else {
var wv = UIWebView()
}
Because my app targets iOS 7, I get a compile-time error:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_WKWebView", referenced from:
__TMaCSo9WKWebView in MyWebKitViewController.o
I tried just assigning the WKWebView to an AnyObject property, but then I can’t call methods on it. So how can I get Swift to just check for these things at runtime?