Reputation: 1120
How do I save/read an NSMutableArray filled with objects I made a class for?
This is what I have so far... (where 'ObjectA' represents my class, and 'objects' is an array containing many instances of 'ObjectA')
//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.");
}
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')
-[__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'
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)?
Thank You!
P.S. My class implements and is NSCoding compliant. If more information is needed please don't hesitate to ask! :)
EDIT -- Here is my ObjectA (per request) (it is a subclass of NSObject)
//
// 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
Upvotes: 0
Views: 144
Reputation: 62686
Since the array contains NSCoding compliant objects, you can use the coding methods, like this...
- (NSMutableArray *)instancesFromArchive {
// compute documentFileName using your original code
if ([[NSFileManager defaultManager] fileExistsAtPath:documentFileName]) {
return [NSKeyedUnarchiver unarchiveObjectWithFile:documentFileName];
} else {
return [NSMutableArray array];
}
}
// archives my array of objects
- (BOOL)archiveInstances {
// compute documentFileName using your original code
return [NSKeyedArchiver archiveRootObject:self.objects toFile:documentFileName];
}
Upvotes: 1
Reputation: 4244
NSNumber class is NSCoding compliant. Are your own objects NSCoding compliant?
Upvotes: 0