mardi 31 mars 2015

Swift nested "if let" seems like madness - is it?

I have been advised to use if lets when examining optional variables. This makes sense, but I have ended up with the following code. It works, but it looks like utter madness! Please tell me that there is a better way.



( 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 )
}
}
}

Aucun commentaire:

Enregistrer un commentaire