检查Swift中是否存在类

时间:2020-11-30 06:31:50

I want to use NSURLQueryItem in my Swift iOS app. However, that class is only available since iOS 8, but my app should also run on iOS 7. How would I check for class existence in Swift?

我想在我的Swift iOS应用程序中使用NSURLQueryItem。但是,该类仅在iOS 8中可用,但我的应用程序也应该在iOS 7上运行。如何在Swift中检查类是否存在?

In Objective-C you would do something like:

在Objective-C中,您可以执行以下操作:

if ([NSURLQueryItem class]) {
    // Use NSURLQueryItem class
} else {
    // NSURLQueryItem is not available
}

Related to this question is: How do you check for method or property existence of an existing class?

与此问题相关的是:如何检查现有类的方法或属性是否存在?

There is a nice section in https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW4 called Supporting Multiple Versions of iOS, which explains different techniques for Objective-C. How can these be translated to Swift?

在https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html#//apple_ref/doc/uid/TP40007072-CH7-SW4中有一个很好的部分称为支持多个iOS的版本,解释了Objective-C的不同技术。怎么能把它们翻译成Swift?

3 个解决方案

#1


14  

Swift 2.0 provides us with a simple and natural way to do this.It is called API Availability Checking.Because NSURLQueryItem class is only available since iOS8.0,you can do in this style to check it at runtime.

Swift 2.0为我们提供了一种简单而自然的方法。它被称为API可用性检查。因为NSURLQueryItem类仅在iOS8.0之后可用,所以你可以用这种方式在运行时检查它。

    if #available(iOS 8.0, *) {
        // NSURLQueryItem is available

    } else {
        // Fallback on earlier versions
    }

#2


13  

Simplest way I know of

我知道的最简单的方式

if NSClassFromString("NSURLQueryItem") != nil {
    println("NSURLQueryItem exists")
}else{
    println("NSURLQueryItem does not exists")
}

#3


9  

Try this:

尝试这个:

if objc_getClass("NSURLQueryItem") != nil {
   // iOS 8 
} else {
   // iOS 7
}

I've also done it like this too:

我也是这样做的:

if let theClass: AnyClass = NSClassFromString("NSURLQueryItem") {
    // iOS 8
} else {
    // iOS 7
}

Or, you can also check system version like so, but this isn't the best practice for iOS dev - really you should check if a feature exists. But I've used this for a few iOS 7 hacks... pragmatism over purity.

或者,你也可以像这样检查系统版本,但这不是iOS开发的最佳实践 - 你应该检查一个功能是否存在。但是我已经将它用于一些iOS 7黑客......实用主义而非纯度。

    switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
    case .OrderedSame, .OrderedDescending:
        iOS7 = false
    case .OrderedAscending:
        iOS7 = true
    }

#1


14  

Swift 2.0 provides us with a simple and natural way to do this.It is called API Availability Checking.Because NSURLQueryItem class is only available since iOS8.0,you can do in this style to check it at runtime.

Swift 2.0为我们提供了一种简单而自然的方法。它被称为API可用性检查。因为NSURLQueryItem类仅在iOS8.0之后可用,所以你可以用这种方式在运行时检查它。

    if #available(iOS 8.0, *) {
        // NSURLQueryItem is available

    } else {
        // Fallback on earlier versions
    }

#2


13  

Simplest way I know of

我知道的最简单的方式

if NSClassFromString("NSURLQueryItem") != nil {
    println("NSURLQueryItem exists")
}else{
    println("NSURLQueryItem does not exists")
}

#3


9  

Try this:

尝试这个:

if objc_getClass("NSURLQueryItem") != nil {
   // iOS 8 
} else {
   // iOS 7
}

I've also done it like this too:

我也是这样做的:

if let theClass: AnyClass = NSClassFromString("NSURLQueryItem") {
    // iOS 8
} else {
    // iOS 7
}

Or, you can also check system version like so, but this isn't the best practice for iOS dev - really you should check if a feature exists. But I've used this for a few iOS 7 hacks... pragmatism over purity.

或者,你也可以像这样检查系统版本,但这不是iOS开发的最佳实践 - 你应该检查一个功能是否存在。但是我已经将它用于一些iOS 7黑客......实用主义而非纯度。

    switch UIDevice.currentDevice().systemVersion.compare("8.0.0", options: NSStringCompareOptions.NumericSearch) {
    case .OrderedSame, .OrderedDescending:
        iOS7 = false
    case .OrderedAscending:
        iOS7 = true
    }