xcode 有没有更好的方法来处理 Swift 嵌套的“如果让”“厄运金字塔”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29364886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a better way of coping with Swift's nested "if let" "pyramid of doom?"
提问by Joseph Beuys' Mum
Is there a better way of dealing with a chain of optional properties than nested if letstatements? I have been advised to use if lets when examining optional properties, which makes sense as it deals with them at compile time rather than run time, but it looks like utter madness! Is there is a better way?
有没有比嵌套if let语句更好的处理可选属性链的方法?有人建议我在检查可选属性时使用 iflets,这是有道理的,因为它在编译时而不是运行时处理它们,但它看起来完全疯狂!有没有更好的方法?
Here is the current "pyramid of doom" I have ended up with, as an example:
这是我最终得到的当前“末日金字塔”,例如:
( users: [ JSONValue ]? ) in
if 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 )
}
}
}
Post-script
后记
Airspeed Velocity's answer below is the right answer, but you will need Swift 1.2 to use multiple lets separated by commas as he suggests, which only currently runs in XCode 6.3, which is in beta.
Airspeed Velocity 下面的答案是正确的答案,但您将需要 Swift 1.2 才能使用他建议的多个由逗号分隔的让,目前仅在测试版的 XCode 6.3 中运行。
回答by Airspeed Velocity
As commenters have said, Swift 1.2 now has multiple-let syntax:
正如评论者所说,Swift 1.2 现在有 multiple-let 语法:
if let jsonValue = users?.first,
json = jsonValue.object,
userIDValue = json[ "id" ],
doubleID = userIDValue.double,
userID = doubleID.map({ String(Int(doubleID))})
{
println( userID )
}
That said, in this instance it looks like you could might be able to do it all via optional chaining in 1.1, depending on what your objects are:
也就是说,在这种情况下,您似乎可以通过 1.1 中的可选链接来完成这一切,具体取决于您的对象是什么:
if let userID = users?.first?.object?["id"]?.double.map({String(Int(if let jsonValue = users?.first,
let json = jsonValue.object,
let userIDValue = json[ "id" ],
let doubleID = userIDValue.double,
let userID = doubleID.map({ String(Int(doubleID))})
{
println( userID )
}
))}) {
println(userID)
}
Note, much better to use first
(if this is an array) rather than [0]
, to account for the possibility the array is empty. And map on the double
rather than !
(which would blow up if the value is not double-able).
请注意,最好使用first
(如果这是一个数组)而不是[0]
,以说明数组为空的可能性。并在double
而不是映射上!
(如果值不是双倍的,它会爆炸)。
回答by Sagar D
UPDATE for Swift-3: The syntax has changed :
Swift-3 更新:语法已更改:
func myFunc(myOptional: Type?) {
if let object = myOptional! {
...
}
}
回答by gzfrancisco
In Swift 2, we have the guard
statement.
在Swift 2 中,我们有guard
语句。
Instead of:
代替:
func myFunc(myOptional: Type?) {
guard array.first else {?return }
}
You can do it like this:
你可以这样做:
##代码##Check http://nshipster.com/guard-and-defer/from NSHipster.
从 NSHipster检查http://nshipster.com/guard-and-defer/。