简体   繁体   English

swift 3与iOS 9上的NSURL类不兼容吗?

[英]Is swift 3 not compatible with NSURL classes on iOS 9?

I am migrating my Swift 2.2 code building and working fine on xcode 7 / ios 8 and ios 9. As of the answer to this question Swift 3 is compatible with iOS 8 and newer. 我正在迁移我的Swift 2.2代码构建并在xcode 7 / ios 8和ios 9上正常工作。截至此问题的答案, Swift 3兼容iOS 8及更新版本。 And I am also aware that I won't be able to use newer APIs. 而且我也知道我将无法使用更新的API。

In my app I am creating some https requests, downloading some files etc. 在我的应用程序中,我正在创建一些https请求,下载一些文件等。

My assumption is that I should also be able to use the classes NSURL, URLSessionDownloadDelegate, NSMutableURLRequest, etc. all available from iOS (8.0 and later). 我的假设是我还应该能够使用iOS(8.0及更高版本)中提供的NSURL,URLSessionDownloadDelegate,NSMutableURLRequest等类。

But either I (obiously) run into a crash running my migrated code on iOS 8.1 because I use the URL class (available from iOS 10) or the compiler complains 'NSURL' is not implicitly convertible to 'URL' (see also this question ). 但是,我(好奇地)遇到在iOS 8.1上运行我的迁移代码的崩溃,因为我使用URL类(可从iOS 10获得)或编译器抱怨'NSURL'不能隐式转换为'URL'(另请参阅此问题 ) 。

Therefore I am not able to use the URL class, if I want to be compatible with iOS 9. 因此,如果我想与iOS 9兼容,我无法使用URL类。

Here is some code to make things clearer: 以下是一些使事情更清晰的代码:

Swift 2: 斯威夫特2:

class ServerCom: NSObject, NSURLSessionDownloadDelegate {
   private var fileURL: NSURL = NSURL()
    [...]       
           guard let urlString: NSURL =
        NSURL(string: params,
            relativeToURL: NSURL(string: dcParam.baseURL) )  else {
                let failed = "Could not generate URL from: \(dcParam.baseURL)\(params)"
                throw fetchAppXmlError.FAILED_URL_GENERATION(failed)
    }
    [...]
    let request = NSMutableURLRequest(URL: urlString)
    request.timeoutInterval = 10
    request.HTTPMethod = "GET"

}

Swift 3 斯威夫特3

class ServerCom: NSObject, URLSessionDownloadDelegate {

 fileprivate var localFileURL: NSURL = NSURL()

 [...]

         guard let urlString: NSURL =
            NSURL(string: params,
                relativeTo: NSURL(string: dcParam.baseURL) )  else {
                    let failed = "Could not generate URL from: \(dcParam.baseURL)\(params)"
                    throw fetchAppXmlError.failed_URL_GENERATION(failed)
        }

 let request = NSMutableURLRequest(url: urlString)
 request.timeoutInterval = 10
 request.httpMethod = "GET"
 [...]

}

In the swift 3 code I get the following errors: 在swift 3代码中,我收到以下错误:

  1. At the line with: let request = ...: 'NSURL' is not implicitly convertible to 'URL'; 在以下行:let request = ...:'NSURL'不能隐式转换为'URL'; did you mean to use 'as' to explicitly convert? 你的意思是使用'as'来明确转换?
  2. At the line with let urlString = ...: Cannot convert value of type 'NSURL?' 在let urlString = ...的行中:无法转换'NSURL?'类型的值 to expected argument type 'URL?' 预期的参数类型'URL?'

I can't use localFileURL with type URL because this would be iOS 10 only. 我不能将localFileURL与类型URL一起使用,因为这只是iOS 10。 I also can't use URLRequest instead of NSMutableURLRequest because it is also only available from iOS (10.0 and later). 我也不能使用URLRequest而不是NSMutableURLRequest,因为它也只能从iOS(10.0及更高版本)获得。 It seems that I always use somehow URLs of type URL rather than the "old" NSURL. 似乎我总是使用URL类型的URL而不是“旧”NSURL。

So my question is: Is there any way to use NSURL et. 所以我的问题是:有没有办法使用NSURL等。 al. 人。 with swift 3 and iOS 9? 使用swift 3 iOS 9?

Or in more general, is there a way to do background downloads with swift 3 that will work on iOS 9 and iOS 10? 或者更一般地说,有没有办法使用可在iOS 9和iOS 10上运行的swift 3进行后台下载?

Does this inconsistency come from the bridging of NSURLComponents to URLComponents? 这种不一致是否来自NSURLComponents与URLComponents的桥接?

Thanks to OOPer i can answer my own question: 感谢OOPer,我可以回答我自己的问题:

the code works fine with the URL class but only when you set the Deplyoment Target to the same iOS Version as your simulator! 代码适用于URL类,但仅当您将Deplyoment Target设置为与模拟器相同的 iOS版本时!

XCode 8 updated my iOS 9 SDK to version 10 and so I only had the choice to use the simulator on either iOS 10 or iOS 8.1. XCode 8将我的iOS 9 SDK更新为版本10,因此我只能选择在iOS 10或iOS 8.1上使用模拟器。 However the Deployment Target was set to iOS 9 (I was pretty sure that it was before the upgrade set to iOS 8.1). 但是,部署目标设置为iOS 9(我很确定它是在升级设置为iOS 8.1之前)。

So I run into the runtime error with an iOS 8.1 simulator and Deployment Target set to iOS 9. 所以我遇到运行时错误,iOS 8.1模拟器和部署目标设置为iOS 9。

But it does work with iOS 8.1 simulator and Deployment Target set to iOS 8.1! 但它确实适用于iOS 8.1模拟器部署目标设置为iOS 8.1!

Here is the Swift 3 Code working also with iOS 8.1: 这是与iOS 8.1一起使用的Swift 3代码:

class ServerCom: NSObject, URLSessionDownloadDelegate {

fileprivate var localFileURL: URL = URL(string: "dummy")!

 [...]

         guard let urlString: URL =
            URL(string: params,
                relativeTo: URL(string: dcParam.baseURL) )  else {
                    let failed = "Could not generate URL from: \(dcParam.baseURL)\(params)"
                    throw fetchAppXmlError.failed_URL_GENERATION(failed)
        }

        var request = URLRequest(url: urlString)
        request.timeoutInterval = 10
        request.httpMethod = "GET"
 [...]

}

Thank you! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM