ios – Swift – NSURL错误

ios – Swift – NSURL错误,第1张

概述尝试使用下面的NSURL类时出错,下面的代码实际上是试图将我从Facebook拉入的图像存储到imageView中.错误如下: value of optional type 'NSURL?' not unwrapped, did you mean to use '!' or '?' 不知道为什么会这样,帮忙! import UIKitclass ViewController: UIViewCo 尝试使用下面的NSURL类时出错,下面的代码实际上是试图将我从Facebook拉入的图像存储到imageVIEw中.错误如下:
value of optional type 'NSURL?' not unwrapped,dID you mean to use '!' or '?'

不知道为什么会这样,帮忙!

import UIKitclass VIEwController: UIVIEwController {    @IBOutlet weak var myImage: UIImageVIEw!    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        let myProfilePictureURL = NSURL(string: "http://graph.facebook.com/bobdylan/picture")        let imageData = NSData(contentsOfURL: myProfilePictureURL)        self.myImage.image = UIImage(data: imageData)    }    overrIDe func dIDReceiveMemoryWarning() {        super.dIDReceiveMemoryWarning()        // dispose of any resources that can be recreated.    }}
解决方法 你正在调用的NSURL构造函数有这个签名:
convenIEnce init?(string URLString: String)

?表示构造函数可能不返回值,因此它被视为可选.

NSData构造函数也是如此:

init?(contentsOfURL url: NSURL)

快速解决方法是:

let myProfilePictureURL = NSURL(string: "http://graph.facebook.com/bobdylan/picture")let imageData = NSData(contentsOfURL: myProfilePictureURL!)self.myImage.image = UIImage(data: imageData!)

最好的解决方案是检查(解包)这些选项,即使您确定它们包含值!

你可以在这里找到更多关于期权的信息:link to official Apple documentation.

总结

以上是内存溢出为你收集整理的ios – Swift – NSURL错误全部内容,希望文章能够帮你解决ios – Swift – NSURL错误所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1107526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存