react-native-userdefaults-ios icon indicating copy to clipboard operation
react-native-userdefaults-ios copied to clipboard

setObjectForKey null value issue

Open melihmucuk opened this issue 10 years ago • 1 comments

If there is null value exist in object, I cant save it.


"user": {
      "Id": "fmkmfkmfsmmfkkm4r43r",
      "phoneNumber": "444543543543",
      "password": null,
      "name": "name",
      "profilePicture": null,
      "email": "[email protected]",
  }

UD.setObjectForKey(this.state.user, 'user');

ERROR on Xcode: Property list invalid for format: 200 (property lists cannot contain objects of type 'CFNull')

melihmucuk avatar Aug 06 '15 15:08 melihmucuk

@melihmucuk Thanks for pointing this out.

Unfortunately, it looks like NSDictionary (which is what a JavaScript object maps to) doesn't support Null values (at least when serializing into NSUserDefaults): https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/#//apple_ref/occ/instm/NSUserDefaults/setObject:forKey:

Of course, I'm sure you solved this by passing in an empty string to those values. I think the proper thing here would be to raise an exception explaining how null values can't be passed in but empty strings, objects, or arrays should be used instead.

I'll try to add this error shortly.

dsibiski avatar Aug 06 '15 22:08 dsibiski