"NSURL? unable to read data" 当我尝试从字符串中获取值时

"NSURL? unable to read data" when I try getting value from String

我正在尝试获取要传递给 NSURL 的字符串,但它 returns

"NSURL? unable to read data"

这是我的代码:

let urlPath: String = "http://test.com/get.php" 
let url: NSURL =NSURL(string: urlPath)! 
let request: NSURLRequest = NSURLRequest(URL: url) 
let connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: true)! 
connection.start()
let urlPath: String = "http://test.com/get.php" 
let url: NSURL =NSURL(string: urlPath)! 

这有效。

可能你的真实 url 是错误的。

这是在模拟器上 运行 时发生的常见问题。 运行 一个设备,您应该会看到 NSURL 已正确创建。