0

I am trying to Change the value of an Object's property (Unarchived from .plist) and it doesn't seem to work... I Am trying to figure out whats wrong for a while now and I would really appreciate any help.

The Code:

NSArray * pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * pathToSave = [[pathArray objectAtIndex:0] stringByAppendingPathComponent:@"ownProfileArchive"];
OwnProfileData * OwnProfile = [NSKeyedUnarchiver unarchiveObjectWithFile:pathToSave];
[OwnProfile setName:[NameField text]]; // at that point, even if I change the name to a string saying "Hello!", It would always be NULL! 
NSLog(@"Name = %@", [OwnProfile Name]); // Here I can see it is always null.
[NSKeyedArchiver archiveRootObject:OwnProfile toFile:pathToSave];

Now I For the first time this runs, It is possible there isn't a file at the path specified, and I assumed the app "wouldn't mind", because documentation says that NSKeyedArchiver will create a file if there isn't one in existent. The question is: If there isn't a file, Will The code for creating the object (NSKeyedUnarchiver) would create a functional object, because it does return a value o 'NULL', but I can't change it... If A file Has To Exist, What is the code for checking if file exists and creating one if it doesn't? (I think there will be a If-Else Conditional involved, but I don't know what the condition is and how to create a new file...).

byteSlayer
  • 1,806
  • 5
  • 18
  • 36

1 Answers1

0

I bet the issue is that if the file does not exist, then I suspect you aren't getting an actual instance of OwnProfileData, and hence OwnProfile is nil. Check for nil, and create a new instance in that case (which can be added to your archive for future use).

Objective-C allows sending messages to nil, so stuff like this is hard to see. Try NSLogging OwnProfile and see!

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • Cheers Helped me a lot... Really hard to debug that kinda staff. – byteSlayer Jul 07 '12 at 19:11
  • Hi, I have tried writing code to check if file exists which looks something like that: BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pathToSave]; Now the thing is, it is always true! it returns true and then proceeds with creating the object from archive, but then it returns null just like before, even though according to the file manager a file is there. So I went on and checked the 'documents' folder of my project and surprise surprise, NO FILES IN THERE. So do you have any advice for that? If no, Is it possible to create a file myself and just let it always be there? – byteSlayer Jul 07 '12 at 19:27
  • Have you inspected the pathToSave variable, and done a sanity check on it? Last time I found myself racking my brain about files existing, I was looking in the wrong simulator's documents folder (i.e. the file existed in my 5.0 but not my 5.1, app was running on 5.0 and I was looking in 5.1)... As for creating your own file; you can absolutely do that :-) But that's probably for another SO question ;-) – Chris Trahey Jul 07 '12 at 19:36