Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use nil instead of Optional(<null>) for null value in JSON

Tags:

json

swift

In Swift, NSJSONSerialization.JSONObjectWithData uses Optional(<null>) for null value in JSON. Is that possible to tell the parse to use nil instead (i.e., drop fields with null values because Swift Dictionary doesn't allow nil value)?

like image 717
Ethan Avatar asked Aug 15 '14 04:08

Ethan


2 Answers

NSJSONSerialization uses NSNull objects, and that's what you're seeing; you can simply check for them by comparing to NSNull().

like image 149
jtbandes Avatar answered Oct 13 '22 05:10

jtbandes


Optional() is an NSNull value. You can avoid error like this:

if !(myField is NSNull) {
    //Whatever you want to do with myField when not null
} else {
    //Whatever you want to do when myField is null
}
like image 37
DarksteelPenguin Avatar answered Oct 13 '22 04:10

DarksteelPenguin