Error[8]: Undefined offset: 3, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

概述有没有比嵌套if语句更好的处理可选属性链的方法?我被建议在检查可选属性时使用if,这有意义,因为它在编译时而不是运行时处理它们,但它看起来像是疯狂!有更好的方法吗? 以下是我最终得到的“厄运金字塔”,例如: ( users: [ JSONValue ]? ) inif let jsonValue: JSONValue = users?[ 0 ]{ if let json: Dicti 有没有比嵌套if语句更好的处理可选属性链的方法?我被建议在检查可选属性时使用if,这有意义,因为它在编译时而不是运行时处理它们,但它看起来像是疯狂!有更好的方法吗?

以下是我最终得到的“厄运金字塔”,例如:

( users: [ JsONValue ]? ) inif let JsonValue: JsONValue = users?[ 0 ]{    if let Json: Dictionary< String,JsONValue > = JsonValue.object    {        if let userIDValue: JsONValue = Json[ "ID" ]        {            let userID: String = String( Int( userIDValue.double! ) )            println( userID )        }    }}

后记

Airspeed VeLocity的答案是正确的答案,但是你需要使用Swift 1.2来使用逗号分隔的多个let,因为他建议,目前只在测试版的XCode 6.3中运行.

解决方法 正如评论者所说,Swift 1.2现在具有多重语法:

if let JsonValue = users?.first,Json = JsonValue.object,userIDValue = Json[ "ID" ],doubleID = userIDValue.double,userID = doubleID.map({ String(Int(doubleID))}){    println( userID )}

也就是说,在这种情况下,看起来您可以通过1.1中的可选链接来完成所有 *** 作,具体取决于您的对象:

if let userID = users?.first?.object?["ID"]?.double.map({String(Int([+++]))}) {    println(userID)}

注意,首先使用(如果这是一个数组)而不是[0]要好得多,以便考虑数组为空的可能性.和地图上的双而不是! (如果价值不是双倍的话会爆炸).

总结

以上是内存溢出为你收集整理的xcode – 有没有更好的方法来应对Swift的嵌套“if let”“厄运金字塔?”全部内容,希望文章能够帮你解决xcode – 有没有更好的方法来应对Swift的嵌套“if let”“厄运金字塔?”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
xcode – 有没有更好的方法来应对Swift的嵌套“if let”“厄运金字塔?”_app_内存溢出

xcode – 有没有更好的方法来应对Swift的嵌套“if let”“厄运金字塔?”

xcode – 有没有更好的方法来应对Swift的嵌套“if let”“厄运金字塔?”,第1张

概述有没有比嵌套if语句更好的处理可选属性链的方法?我被建议在检查可选属性时使用if,这有意义,因为它在编译时而不是运行时处理它们,但它看起来像是疯狂!有更好的方法吗? 以下是我最终得到的“厄运金字塔”,例如: ( users: [ JSONValue ]? ) inif let jsonValue: JSONValue = users?[ 0 ]{ if let json: Dicti 有没有比嵌套if语句更好的处理可选属性链的方法?我被建议在检查可选属性时使用if,这有意义,因为它在编译时而不是运行时处理它们,但它看起来像是疯狂!有更好的方法吗?

以下是我最终得到的“厄运金字塔”,例如:

( users: [ JsONValue ]? ) inif let JsonValue: JsONValue = users?[ 0 ]{    if let Json: Dictionary< String,JsONValue > = JsonValue.object    {        if let userIDValue: JsONValue = Json[ "ID" ]        {            let userID: String = String( Int( userIDValue.double! ) )            println( userID )        }    }}

后记

Airspeed VeLocity的答案是正确的答案,但是你需要使用Swift 1.2来使用逗号分隔的多个let,因为他建议,目前只在测试版的XCode 6.3中运行.

解决方法 正如评论者所说,Swift 1.2现在具有多重语法:

if let JsonValue = users?.first,Json = JsonValue.object,userIDValue = Json[ "ID" ],doubleID = userIDValue.double,userID = doubleID.map({ String(Int(doubleID))}){    println( userID )}

也就是说,在这种情况下,看起来您可以通过1.1中的可选链接来完成所有 *** 作,具体取决于您的对象:

if let userID = users?.first?.object?["ID"]?.double.map({String(Int())}) {    println(userID)}

注意,首先使用(如果这是一个数组)而不是[0]要好得多,以便考虑数组为空的可能性.和地图上的双而不是! (如果价值不是双倍的话会爆炸).

总结

以上是内存溢出为你收集整理的xcode – 有没有更好的方法来应对Swift的嵌套“if let”“厄运金字塔?”全部内容,希望文章能够帮你解决xcode – 有没有更好的方法来应对Swift的嵌套“if let”“厄运金字塔?”所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1037613.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存