Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring a NSArray of my Custom Objects in NSUserDefaults
    primarykey
    data
    text
    <p>The requirement is that I want to store <code>NSArray</code> of my custom objects in <code>NSUserDefaults</code>. Following is the code from my example</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; sampleArray=[[NSMutableArray alloc]init]; MyClass *obj1=[[MyClass alloc]init]; obj1.name=@"Reetu"; obj1.countOpen=1; NSArray *subArray = [[NSArray alloc]initWithObjects:@"likes131",@"likes132", @"likes133", nil]; obj1.hobbies = [[NSArray alloc]initWithObjects:@"like11", @"like12", subArray, nil]; [sampleArray addObject:obj1]; MyClass *obj2=[[MyClass alloc]init]; obj2.name=@"Pinku"; obj2.countOpen=2; NSArray *subArray2 = [[NSArray alloc]initWithObjects:obj1 ,@"likes231",@"likes232", @"likes233", obj1 ,nil]; obj2.hobbies = [[NSArray alloc]initWithObjects:@"like21", @"like22", subArray2 ,nil]; [sampleArray addObject:obj2]; MyClass *obj3=[[MyClass alloc]init]; obj3.name=@"Mike"; obj3.countOpen=6; obj3.hobbies = [[NSArray alloc]initWithObjects:obj1 , obj2 ,@"likes000", nil]; [sampleArray addObject:obj3]; //First lets encode it NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults]; NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:sampleArray]; [userDefault setObject:myEncodedObject forKey:[NSString stringWithFormat:@"sample"]]; [userDefault synchronize]; //Lets decode it now NSData *myDecodedObject = [userDefault objectForKey: [NSString stringWithFormat:@"sample"]]; NSArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: myDecodedObject]; //Print the array received from User's Default for (MyClass *item in decodedArray) { NSLog(@"name=%@",item.name); NSLog(@"LIKES TO %@",item.hobbies); } } </code></pre> <p>This is my custom class confirming to the NSCoding protocol</p> <pre><code>- (void)encodeWithCoder:(NSCoder *)encoder { //Encode properties, other class variables, etc [encoder encodeObject:self.name forKey:@"name"]; [encoder encodeObject:[NSNumber numberWithInt:self.countOpen] forKey:@"destinationCode"]; [encoder encodeObject:self.hobbies forKey:@"likesTo"]; } - (id)initWithCoder:(NSCoder *)decoder { self = [super init]; if( self != nil ) { self.name = [decoder decodeObjectForKey:@"name"]; self.countOpen = [[decoder decodeObjectForKey:@"countOpen"] intValue]; self.hobbies = [decoder decodeObjectForKey:@"likesTo"]; } return self; } </code></pre> <p>here is the output:-</p> <pre><code>2013-10-22 17:01:47.118 Sample[1056:c07] name=Reetu 2013-10-22 17:01:47.120 Sample[1056:c07] LIKES TO ( like11, like12, ( likes131, likes132, likes133 ) ) 2013-10-22 17:01:47.121 Sample[1056:c07] name=Pinku 2013-10-22 17:01:47.123 Sample[1056:c07] LIKES TO ( like21, like22, ( "&lt;MyClass: 0x6e32910&gt;", likes231, likes232, likes233, "&lt;MyClass: 0x6e32910&gt;" ) ) 2013-10-22 17:01:47.125 Sample[1056:c07] name=Mike 2013-10-22 17:01:47.127 Sample[1056:c07] LIKES TO ( "&lt;MyClass: 0x6e32910&gt;", "&lt;MyClass: 0x6e1f610&gt;", likes000 ) </code></pre> <p>The problem is <code>&lt;MyClass: 0X632910&gt;</code>. I was expecting it to be contents of <code>obj1</code> itself.</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.
 

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