Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving an NSMutableArray filled with custom objects
    text
    copied!<p>How do I save/read an NSMutableArray filled with objects I made a class for?</p> <p>This is what I have so far... (where 'ObjectA' represents my class, and 'objects' is an array containing many instances of 'ObjectA')</p> <pre><code>//Creating a file //1) Search for the app's documents directory NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; //2) Create the full file path by appending the desired file name NSString *documentFileName = [documentsDirectory stringByAppendingPathComponent:@"save.dat"]; //Load the array objects = [[NSMutableArray alloc] initWithContentsOfFile: _documentFileName]; if(objects == nil) { //Array file didn't exist... create a new one objects = [[NSMutableArray alloc]init]; NSLog(@"Did not find saved list, Created new list."); } else { NSLog(@"Found saved list, Loading list."); } </code></pre> <p>This will load an array if it exists. It works if the array is filled with property type objects like NSNumbers, I tested that. If I fill it with my own objects, it crashes! This is the error: (where 'objectAInt' is a private int belonging to 'ObjectA')</p> <pre><code>-[__NSCFNumber objectAInt]: unrecognized selector sent to instance 0x15d417f02013-11-19 17:28:58.645 My Project[1791:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber objectAInt]: unrecognized selector sent to instance 0x15d417f0' </code></pre> <p>What do I need to do to make my class, 'ObjectA', work with the save/read process like it does with NSNumbers and other NS objects of type (id)?</p> <p>Thank You!</p> <p>P.S. My class implements and is NSCoding compliant. If more information is needed please don't hesitate to ask! :)</p> <p>EDIT -- Here is my ObjectA (per request) (it is a subclass of NSObject)</p> <pre><code>// // ObjectA.m // My Project // // Created by Will Battel on 8/8/13. // // #import "ObjectA.h" @implementation ObjectA @synthesize objectAString, objectAString2, objectAString3, objectAInt; -(id)initWithName:(NSString *)_objectAString{ self = [super init]; objectAString = _objectAString; objectAInt = 2; objectAString3 = @"$0.00"; return self; } -(id)initWithCoder:(NSCoder *)decoder { self = [super init]; if (self) { NSLog(@"Decoding"); [self setObjectAString:[decoder decodeObjectForKey:@"objectAStringKey"]]; [self setObjectAString2:[decoder decodeObjectForKey:@"objectAString2Key"]]; [self setObjectAString3Price:[decoder decodeObjectForKey:@"objectAString3Key"]]; [self setobjectAInt:[decoder decodeIntForKey:@"objectAIntKey"]]; } return self; } -(void)encodeWithCoder:(NSCoder *)encoder { NSLog(@"Encoding"); [encoder encodeObject:objectAString forKey:@"objectAStringKey"]; [encoder encodeObject:objectAString2 forKey:@"objectAString2Key"]; [encoder encodeObject:objectAString3 forKey:@"objectAString3Key"]; [encoder encodeInt:objectAInt forKey:@"objectAIntKey"]; } @end </code></pre>
 

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