Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you only store standard objects inside the dictionary like NSData, NSString, NSNumber, NSDate, NSArray, NSDictionary or a combination of them you don't have to do anything special. </p> <p>However, if you have instances of custom objects in it (i.e. classes that you've created) you first need to convert it into a compatible type (e.g. <code>NSData</code>). You can do this using the code below:</p> <pre><code>NSData* data=[NSKeyedArchiver archivedDataWithRootObject:json]; [NSUserDefaults standardUserDefaults] setObject:data forKey:@"user"] </code></pre> <p>For this method to work, you ned to implement these 2 methods in the custom classes you are trying to save:</p> <pre><code>- (id)initWithCoder:(NSCoder*)coder - (void)encodeWithCoder:(NSCoder*)coder; </code></pre> <p>To get the dictionary back from NSUserDefaults (decode) you can use:</p> <pre><code>NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:@"user"]; NSDictionary* json = [NSKeyedUnarchiver unarchiveObjectWithData:data]; </code></pre> <p><strong>EDIT</strong></p> <p>To check if your <code>json</code> object contains any <code>[NSNull null]</code> values, add this piece of code before you are making your insert into <code>NSUserDefaults</code></p> <pre><code>for (id val in [json allValues]) { if ([val isKindOfClass:[NSNull class]]) { NSLog(@"This bad! NSNull should not be in the dictionary"); break; } } </code></pre> <p>If you get any <code>This is bad...</code> messages in the console, then you have 2 choices. 1. Use the archiving/unarchiving method I described above 2. Replace the <code>NSNull</code> objects from the dictionary with other values (e.g. empty strings) if this does not break your code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload