栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何从字符串值Swift中删除Optional

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何从字符串值Swift中删除Optional

就像@GioR所说的那样,该值是Optional(52.523553),因为latstring的类型是隐式的:String?。这是由于let lat =
data.value(forKey:“ lat”)将返回字符串?隐式设置lat的类型。
有关值的文档,请参阅https://developer.apple.com/documentation/objectivec/nsobject/1412591-value(forKey
:)


Swift有多种处理nil的方法。这三个可能对您有帮助,nil合并运算符:

??

如果可选结果为nil,则此运算符将提供默认值:

let lat: String = data.value(forKey: "lat") ?? "the lat in the dictionary was nil!"

警卫声明

guard let lat: String = data.value(forKey: "lat") as? String else {    //Oops, didn't get a string, leave the function!}

Guard语句使您可以将可选变量转换为等效的非可选变量,如果恰好为nil,则可以退出该函数

如果让

if let lat: String = data.value(forKey: "lat") as? String {    //Do something with the non-optional lat}//Carry on with the rest of the function

希望这可以帮助^^



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/409697.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号