Read through this code. I wrote it at my desk in 5 minutes, it’s not production code but just for show. Then read my question at the bottom:
@interface SomeClass : NSObject {
NSString *currentFruit;
NSMutableArray *fruits;
}
@property (retain) NSString *currentFruit;
@property (retain) NSMutableArray *fruits;
@end
@implementation SomeClass
@synthesize currentFruit, fruits;
-(void) createArray {
self.fruits = [[NSMutableArray alloc] initWithObjects:@"apples",@"oranges",@"grapes",nil];
self.currentFruit = [fruits objectAtIndex:0];
}
-(void)writeToDisk {
NSString *filePath = @"/somePath/file.ext";
NSMutableData *fileData = [NSMutableData data];
NSKeyedArchiver *coder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:fileData];
[coder encodeObject:self.fruits forKey:@"fruitArray"];
[coder encodeObject:self.currentFruit forKey:@"theFruit"];
[coder finishEncoding];
[fileData writeToFile:filePath atomically:YES];
[coder release];
}
-(void)loadDataFromDisk {
NSString *filePath = @"/somePath/file.ext";
NSData *fileData = [[NSData alloc] initWithContentsOfFile:filePah];
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:fileData];
NSMutableArray *decodedFruits = [decoder decodeObjectForKey:@"fruitArray"];
NSString *decodedFruit = [decoder decodeObjectForKey:@"theFruit"];
self.currentFruit = decodedFruit;
self.fruits = decodedFruits;
[decoder release];
[fileData release];
}
Say I used the method -(void)createArray to make my array and then set the instance of currentFruit to the first object in the array. Then I used -(void)writeDataToDisk and stored the objects to file. When I use loadDataFromDisk, does my application still know that currentFruit should be the object at the beginning of my fruits array? or will I have two separate instances now?
I’m trying to figure this out. Should I be keeping track of the array index instead of tracking actual objects? I want to always be referencing the object in the array. Does this make sense?
EDIT: I withdraw the following answer, having somehow not realised that you’re not using NSCoder. The below applies to encoding by implementing NSCoding and supplying a single root object only. If what you want works in your case, it’ll be an implementation detail outside of the documentation and therefore not to be relied upon.
I am inclined to believe that you’ll get the same object; the archivers detect when you write out multiple instances of the same object and use a link back to the original. Otherwise they’d be unable to write any hierarchy of objects that included a circular reference — they’d just write forever to the disk. Therefore, the information that the object ‘theFruit’ is from the array ‘fruitArray’ is encoded in the archive. See ‘Root Object’ within the Archives and Serialization Programming Guide.
Given that you keep the same instance of NSKeyedUnarchiver, it’s not unreasonable to assume that the code underneath will be smart enough to return the same object. However, I am reading between the lines on Apple’s focus of storing an object graph and can’t find an explicit acknowledgement of this.