ios 解包可选值时意外发现 nil
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26675490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unexpectedly found nil while unwrapping optional value
提问by Ben Gray
In my app I am checking to see whether a post has a picture or not.
在我的应用程序中,我正在检查帖子是否有图片。
For this I am using:
为此,我正在使用:
if pictures[string]? != nil {
if var image: NSData? = pictures[string]? {
imageView.image = UIImage(data: image!)
}
}
However, it still comes up with the error:
但是,它仍然出现错误:
fatal error: unexpectedly found nil while unwrapping an Optional value.
致命错误:在展开 Optional 值时意外发现 nil。
I'm sure it is something easy to fix but I am quite new to this - what am I doing wrong?
我确定这很容易解决,但我对此很陌生-我做错了什么?
回答by Anorak
Try doing it this way:
尝试这样做:
if let imageData = pictures[string] {
if let image = UIImage(data: imageData) {
imageView.image = image
}
}
Assuming that string
is a valid key.
假设这string
是一个有效的密钥。
You are dealing with optionals, so conditionally unwrap each return object before using it.
您正在处理可选项,因此在使用它之前有条件地解开每个返回对象。
Forced unwrapping is dangerous and should only be used when you are absolutelysure that an optional contains a value. Your imageData may not be in the correct format to create an image, but you are forcibly unwrapping it anyway. This is okay to do in Objective-C as it just means nil
objects get passed around. Swift is not so tolerant.
强制解包是危险的,只有在您绝对确定一个可选项包含一个值时才应该使用。您的 imageData 可能不是创建图像的正确格式,但无论如何您都在强行解开它。在 Objective-C 中这样做是可以的,因为它只是意味着nil
对象会被传递。斯威夫特就没有那么宽容了。
回答by iOS Developer
It is the issue of swift when you forget to wrap optional values
当您忘记包装可选值时,这是 swift 的问题
Replace line imageView.image = UIImage(data: image!)
with imageView?.image = UIImage(data: image!)
将行替换imageView.image = UIImage(data: image!)
为imageView?.image = UIImage(data: image!)
回答by Varun Naharia
I faced the same problem with this code
我在这段代码中遇到了同样的问题
if(!placeholderColor.isEqual(nil))
{
self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: [NSForegroundColorAttributeName : placeholderColor])
}
and solved by this
并由此解决
if let placeColor = placeholderColor
{
self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: [NSForegroundColorAttributeName : placeColor])
}
回答by Edgar N
First Double check the format of the base64 string. My string had the following format: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAYAAADnRuK4AAAZDElEQVR42u1daWATZRp+JkfT+76g90ELtKUX5wICshREgYKCKAiCC4iCu+Iq6q6CoKuLF4Igl6jLDaKgiJwiN5SjQAulpTe97zY90qRJ9sc
but everthing before the the comma is not required. I got the code working by changing the format to:iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAYAAADnRuK4AAAZDElEQVR42u1daWATZRp+JkfT+76g90ELtKUX5wICshREgYKCKAiCC4iCu+Iq6q6CoKuLF4Igl6jLDaKgiJwiN5SjQAulpTe97zY90qRJ9sc
首先仔细检查base64 字符串的格式。我的字符串具有以下格式: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAYAAADnRuK4AAAZDElEQVR42u1daWATZRp+JkfT+76g90ELtKUX5wICshREgYKCKAiCC4iCu+Iq6q6CoKuLF4Igl6jLDaKgiJwiN5SjQAulpTe97zY90qRJ9sc
但不需要逗号之前的所有内容。我通过将格式更改为以下代码来运行代码:iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAYAAADnRuK4AAAZDElEQVR42u1daWATZRp+JkfT+76g90ELtKUX5wICshREgYKCKAiCC4iCu+Iq6q6CoKuLF4Igl6jLDaKgiJwiN5SjQAulpTe97zY90qRJ9sc