2

I'm trying to save an NSMutableArray called queueArray so it can be loaded again after the app has been quit. I used a few tutorials to get me going and this is the code I have come up with. The problem seems to be that "initWithCoder" and "encodeWithCoder" are not being called, shown by no NSLog calls and no stopping at breakpoints. I have added the NSCoding protocol to the .h file and I know that queueArray is not nil and it contains MPMediaItems. Here is some of the code I use to try to save and load the array:

-(IBAction)saveQueuePressed {

     NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];

     //should cause encodeWithCoder to be called
     [NSKeyedArchiver archiveRootObject:queueArray toFile:filePath];

}

-(IBAction)loadQueuePressed {

     NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];

     //should cause initWithCoder to be called
     queueArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];

}

-(void)encodeWithCoder:(NSCoder *)coder {

     NSLog(@"encodeWithCoder");

     [coder encodeObject:queueArray forKey:@"savedQueueArray"];
}



-(id)initWithCoder:(NSCoder *)decoder {

     NSLog(@"initWithCoder");

     queueArray = [decoder decodeObjectForKey:@"savedQueueArray"];

     return self;

}

Any help will be greatly appreciated!

1
  • Use the "101010" button to format code. Commented Sep 20, 2010 at 17:06

2 Answers 2

1

The encodeWithCoder: and initWithCoder methods are called when you archive/unarchive an object that responds to them. From what I understand, you have those methods in your class, but the object you are actually archiving (queueArray) is not an instance of that class, it's an NSMutableArray.

If you do want to save your entire object, you can change your saveQueue method to this

-(IBAction)saveQueuePressed {

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];

    // saving the array shouldn't, but saving the self object
    //should cause encodeWithCoder to be called:
    [NSKeyedArchiver archiveRootObject:self toFile:filePath];
}

But if you just want to save the array, I guess you can just use saveQueuePressed and loadQueuePressed, I don't think you need the encode/init WithCoder: methods

Update: Maybe your path is not right. Try

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [[rootPath stringByAppendingPathComponent:@"queueArray.archive"] stringByExpandingTildeInPath]];
Sign up to request clarification or add additional context in comments.

1 Comment

I have changed the code to: //to save NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filePath = [NSString stringWithFormat:@"%@/queueArrayFile", rootPath]; [queueArray writeToFile:filePath atomically:NO]; //to retrieve NSMutableArray *retrievedArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; However, it is still not working as the array is empty when I retrieve it. I dont think I am able to save an MSMutableArray full of MPMediaItems. What should I do?
0

Filipe is right! Your comment said you still didn't use his method.

I had this issue too. Switching from the dictionary's atomic write method to the keyedArchiver fixed it, luckily I only had to change one thing, the line that said writeToFile: is now the archive function.

Now my program's working. For some reason, even when responding to NSCoding, the custom object is not being encoded and breaks my dictionary. Is this a bug with iOS? I've read a fair number of Apple Manuals, but I've also seen a fair number of typos and missing info (For example, try MKMapRect functions without the videos to explain them), or Core Animations referencing the Run Loop before you learn threading, I could go on, half finished sentences in Quartz... so yeah, I've read the manuals and this perplexes me, we have to get a more open iOS SDK at some point, hopefully

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.