Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 可选类型字符串的值?没有打开包装;你的意思是用“我的”吗&引用;或者“是什么?”&引用;?_Swift_Optional - Fatal编程技术网

Swift 可选类型字符串的值?没有打开包装;你的意思是用“我的”吗&引用;或者“是什么?”&引用;?

Swift 可选类型字符串的值?没有打开包装;你的意思是用“我的”吗&引用;或者“是什么?”&引用;?,swift,optional,Swift,Optional,我知道关于Swift中的这个错误报告已经有很多问题了,但是作为一个初学者,我想问一下这个错误是什么意思,以及如何在代码中修复它 required convenience init?(coder aDecoder: NSCoder) { let name = aDecoder.decodeObjectForKey(PropertyKey.nameKey) as! String // Because photo is an optional property of Meal, us

我知道关于Swift中的这个错误报告已经有很多问题了,但是作为一个初学者,我想问一下这个错误是什么意思,以及如何在代码中修复它

required convenience init?(coder aDecoder: NSCoder) {
    let name = aDecoder.decodeObjectForKey(PropertyKey.nameKey) as! String

    // Because photo is an optional property of Meal, use conditional cast.
    let characterise = aDecoder.decodeObjectForKey(PropertyKey.characteriseKey) as? String

    let photo = aDecoder.decodeObjectForKey(PropertyKey.photoKey) as? UIImage

    let rating = aDecoder.decodeIntegerForKey(PropertyKey.ratingKey)

    // Must call designated initializer.
    self.init(name: name, characterise: characterise, photo: photo, rating: rating)
}

我不知道这是不是真的,但我认为
characterize
参数有问题吗?

嗯。可选值可以有实际值或零。如果您要为一个不以可选参数为参数的函数提供可选参数,则必须将其展开

一般而言:


func函数(参数:字符串)
哪一行给出消息?这是说,您正在为非可选变量分配一个可选值,并将其展开。在Swift中,可选值是非常重要的一部分,您应该只看一些有关它们的教程。
if let characterise = characterise {
 self.init(name: name, characterise: characterise, photo: photo, rating: rating) }