致命错误:意外地找到nil,同时解开一个可选的值(是的,再次) [英] fatal error: unexpectedly found nil while unwrapping an Optional value (yes, again)
本文介绍了致命错误:意外地找到nil,同时解开一个可选的值(是的,再次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
似乎发生在
let voca = Vocaloid(name: name!, vocaloidName: vocaID!)
和
parseVocaloidCSV()
任何想法?
这个错误总是发现我的生活。我深深的讨厌
This error always finds away into my life. I hate it deeply.
推荐答案
如果您使用!
尝试用?
替换所有!
,并获取您的代码进行编译。您可能需要重新排列一些东西。
if you use !
you can get these. Try replacing all !
with ?
and getting your code to compile. You may have to rearrange things a bit.
有时您可以使用 ??
nil合并运算符。例如,在您的代码中,如果 name
是 nil ,可以将其替换为默认值(例如 no name
?
Sometimes you can use the ??
nil coalescing operator. For example, in your code, if name
is nil, is it acceptable to replace it with a default value such as "no name"
?
let voca = Vocaloid(name: name ?? "no name", vocaloidName: vocaID ?? -1 )
或正如Midhun MP所建议的,当任何其他参数为nil时,使 voca
= nil
let voca:Vocaloid?
if let name = name, vocaID = vocaID
{
voca = Vocaloid( name:name, vocaloidName:vocaID )
}
else
{
voca = nil
}
或: / p>
or:
let voca = (name != nil && vocaID != nil) ? Vocaloid( name:name!, vocaloidName:vocaID! ) : nil
这篇关于致命错误:意外地找到nil,同时解开一个可选的值(是的,再次)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文