2016-12-07 13 views
0

でURLを使用して画像を表示するために、私は画像を表示しようとしましたが、次のエラーが発生します。リンゴのドキュメントからスウィフト3:どのようにAPI

error : fatal error: unexpectedly found nil while unwrapping an Optional value

var imageURL:UIImageView! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    let url = NSURL(string:"http://cdn.businessoffashion.com/site/uploads/2014/09/Karl-Lagerfeld-Self-Portrait-Courtesy.jpg") 
    let data = NSData(contentsOfURL:url!) 
    if data!= nil { 
     imageURL.image = UIImage(data:data!) 
    } 
} 
+0

これは重複していないと思います。 NSURLは、アンラップを強制しないものではありません。そのNSData(contentsOfURL:url!)の誤用は直観的ではありません。なぜなら、このメソッドが実際に何をしているのかは明らかではないからです。 –

答えて

0

を:

Important

Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.

Instead, for non-file URLs, consider using the dataTaskWithURL:completionHandler: method of the NSURLSession class. See URL Session Programming Guide for details.

たぶん考えますこの作業にAlamofireを使用していますか?

Alamofire.request(.GET, "http://cdn.businessoffashion.com/site/uploads/2014/09/Karl-Lagerfeld-Self-Portrait-Courtesy.jpg").response { (request, response, data, error) in 
    self.imageURL.image = UIImage(data: data, scale:1) 
}