Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map SwiftyJSON to array

let json = JSON(response.result.value!)

let things = json.array.map { jsonThing in          
   Thing()                    
}!

json.array returns an array of one hundred JSONs. After the call to map, I end up with a single Thing.

Why don't I have a new array of Things?

like image 214
Ian Warburton Avatar asked Oct 27 '15 16:10

Ian Warburton


1 Answers

SwiftyJSON has two kinds of getters: optional and non-optional.

The non-optional getters have the "...Value" name syntax.

Optional:

json.array

Non-optional:

json.arrayValue

Be careful, though: if you're using the non-optional getters with a nil value it will crash. If the value can be nil it's better to use the optional getter and safely unwrap with if let (optional binding) or any other known technique: optional chaining, nil coalescing, etc.

like image 74
Eric Aya Avatar answered Sep 22 '22 14:09

Eric Aya