当我尝试从String获取值时,“NSURL?无法读取数据”

时间:2015-10-18 19:12:36

标签: ios swift nsurl

我正在尝试将字符串传递给NSURL,但它返回

“NSURL?无法读取数据”

这是我的代码:

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()

2 个答案:

答案 0 :(得分:0)

let urlPath: String = "http://test.com/get.php" 
let url: NSURL =NSURL(string: urlPath)! 

这有效。

可能你的真实网址是错误的。

答案 1 :(得分:0)

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