致命错误:意外地找到nil,同时解开一个可选的值(是的,再次)

编程入门 行业动态 更新时间:2024-10-27 01:26:28
本文介绍了致命错误:意外地找到nil,同时解开一个可选的值(是的,再次)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

pastebin/AC5qJ1b3

似乎发生在

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,同时解开一个可选的值(是的,再次)

本文发布于:2023-11-26 12:27:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633931.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   错误   意外地   nil

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!