尝试从网址读取文本时出现致命错误

时间:2015-05-28 17:22:41

标签: ios swift nsurlsession

我正在尝试使用Read a text file with NSURL in Swift中的代码,但是当我这样做时,我会收到致命的错误:

unexpectedly found nil while unwrapping an Optional value 

在声明urlContents

的行上
  

代码

let messageURL = NSURL(string: "http://localhost:8888/text.txt")
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(messageURL!,
        completionHandler: {
            (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in

            var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil)

            println(urlContents)

    })
downloadTask.resume()

0 个答案:

没有答案
相关问题