Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Ios Swift 4将nil jsonElement更改为空字符串_Ios_Json_Swift_Xcode - Fatal编程技术网

Ios Swift 4将nil jsonElement更改为空字符串

Ios Swift 4将nil jsonElement更改为空字符串,ios,json,swift,xcode,Ios,Json,Swift,Xcode,根据标题,我在这方面运气不好,我成功地对nil元素进行了一个小修复,将任何可能为nil的内容放入自己的if let,但我希望它成为一个空字符串,而不是nil,因为我以后需要使用它。(我想是吧?) 我对iOS编程很在行,请温柔一点。我试过阅读类似的主题,但没能成功。 谢谢请提供一个默认值,例如: student.studentID = (jsonElement["S_ID"] as? String) ?? "" 请阅读Swift中“零聚结运算符”部分的更多内容: 如果可选的a包含值,则nil合并

根据标题,我在这方面运气不好,我成功地对nil元素进行了一个小修复,将任何可能为nil的内容放入自己的
if let
,但我希望它成为一个空字符串,而不是nil,因为我以后需要使用它。(我想是吧?)

我对iOS编程很在行,请温柔一点。我试过阅读类似的主题,但没能成功。
谢谢

请提供一个默认值,例如:

student.studentID = (jsonElement["S_ID"] as? String) ?? ""
请阅读Swift中“零聚结运算符”部分的更多内容:

如果可选的
a
包含值,则nil合并运算符
(a??b)
将其展开,如果a为
nil
,则返回默认值
b
表达式
a
始终是可选类型。
表达式
b
必须与
a
中存储的类型匹配


您可以使用
else
部件来处理
nil

if let mother = jsonElement["Mother"] as? String {
    student.mother = mother
} else {
    student.mother = ""
}
if let mother = jsonElement["Mother"] as? String {
    student.mother = mother
} else {
    student.mother = ""
}