Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
I am trying to save edited Array back to the plist... this code worked fine in ios3.2 in simulator...

Now in ios4.1 (while debugging) when i click the exit button on simulator (and not the Task red button on xcode) it saves to plist and again when i click
the app icon on simulator, it shows the saved changes...........but when i terminate the app by clicking the Task button and restart the app, it doesn't shows the changes....

i have the following code....

Code:
-(void)applicationWillTerminate:(UIApplication *)application{
called save function
}
- (void)applicationDidEnterBackground:(UIApplication *)application{
called save function
}

+ (void)save
{
	//save contacts list
	NSString *path = [[self class] pathForDocumentWithName:@"Contacts.plist"];
	NSString *plist = [listOfTempContacts description];
	
	NSError *error = nil;
	[plist writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
	
	if (error)
	{
		NSLog(@"Error writing file at path: %@; error was %@", path, error);
	}
	
	//save favourite contacts list
	path=nil;
	plist=nil;
	path = [[self class] pathForDocumentWithName:@"FavContacts.plist"];
	plist = [listOfTempFavContacts description];
	
	error = nil;
	[plist writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
	if (error)
	{
		NSLog(@"Error writing file at path: %@; error was %@", path, error);
	}
	
}


Thank you for reading
 
What are the paths?


Code:
+(void)initialize{

	if (listOfContacts == nil)
    {
        NSString *path = [[self class] pathForDocumentWithName:@"Contacts.plist"];
		 //NSMutableArray *listOfTempContacts = [NSMutableArray arrayWithContentsOfFile:path];
        
        if (listOfTempContacts == nil)
        {
            NSLog(@"Unable to read plist file at path: %@", path);
            path = [[NSBundle mainBundle] pathForResource:@"Contacts"
                                                   ofType:@"plist"];
            
        }
		listOfTempContacts=[[NSMutableArray alloc] initWithContentsOfFile:path];
		//Initialize the merge and sectioned array
		listOfContacts = [[NSMutableArray alloc] init];
		[self showContacts];
	}
	if (listOfFavContacts == nil)
    {
        NSString *favpath = [[self class] pathForDocumentWithName:@"FavContacts.plist"];
		//NSMutableArray *listOfTempContacts = [NSMutableArray arrayWithContentsOfFile:path];
        
        if (listOfTempFavContacts == nil)
        {
            NSLog(@"Unable to read plist file at path: %@", favpath);
            favpath = [[NSBundle mainBundle] pathForResource:@"FavContacts"
                                                   ofType:@"plist"];
           
        }
		 listOfTempFavContacts = [[NSMutableArray alloc] initWithContentsOfFile:favpath];
		//Initialize the merge and sectioned array
		listOfFavContacts = [[NSMutableArray alloc] init];
		[self showFavContacts];
	}
	if (listOfSortContacts==nil) {
		listOfSortContacts=[[NSMutableArray alloc]init];
	}
	if (listOfSortFavContacts==nil) {
		listOfSortFavContacts=[[NSMutableArray alloc]init];
	}
}

+ (NSString *)pathForDocumentWithName:(NSString *)documentName
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, 
														 YES);
	
    NSString *path = [paths objectAtIndex:0];
    
    return [path stringByAppendingPathComponent:documentName];
}


from the console
Unable to read plist file at path: /Users/admin/Library/Application Support/iPhone Simulator/4.1/Applications/7C28813F-206D-408E-987C-3FA5E2343446/Documents/Contacts.plist
 
So is there a file at that path?

Thank you, you asked this question....
Now it is working fine by commenting the orange code...

Code:
if (listOfFavContacts == nil)
    {
        NSString *favpath = [[self class] pathForDocumentWithName:@"FavContacts.plist"];
		//NSMutableArray *listOfTempContacts = [NSMutableArray arrayWithContentsOfFile:path];
        
		
       [COLOR="DarkOrange"] if (listOfTempFavContacts == nil)
        {
            NSLog(@"Unable to read plist file at path: %@", favpath);
            favpath = [[NSBundle mainBundle] pathForResource:@"FavContacts" ofType:@"plist"];
           
        } [/COLOR]
		          NSLog(@"NEw File at path: %@", favpath);
		 listOfTempFavContacts = [[NSMutableArray alloc] initWithContentsOfFile:favpath];
		//Initialize the merge and sectioned array
		listOfFavContacts = [[NSMutableArray alloc] init];
		[self showFavContacts];
	}

The below code searched the file first in the main bundle directory where it finds one coz of which it showed the contents of existing file and not the edited file
Code:
[[NSBundle mainBundle] pathForResource:@"FavContacts" ofType:@"plist"];

/Users/admin/Library/Application Support/iPhone Simulator/4.1/Applications/7C28813F-206D-408E-987C-3FA5E2343446/Documents/FavContacts.plist
2010-11-12 17:39:48.522 Company Contacts Directory[2179:207] NEw File at path: /Users/admin/Library/Application Support/iPhone Simulator/4.1/Applications/7C28813F-206D-408E-987C-3FA5E2343446/Company Contacts Directory.app/FavContacts.plist
 
For what it's worth, I believe the method you're using to write to file is actually deprecated after iOS 3.2. The only thing I use writeToFile: for now is for blanking out a file :) Check out NSFileHandle and it's writeData method.

EDIT:
Actually, maybe it's only deprecated for NSString. I just remembered I use it for writing out to a plist, but my data for the plist is an instance of NSData. Try and convert your data to NSData (should be pretty easy) and change the receiver of writeToFile to be the NSData instead of the NSString. The section of my app where I use NSFileHandle instead of writeToFile involved strings, so that's probably why I switched to using that instead in that case. So you can either switch to NSFileHandle and writeData, or convert your plist data into a NSData object and try again.
 
Last edited by a moderator:
From method documentation for writeToFile:atomically: from the Class Reference for NSString:
(Deprecated in iOS 2.0. Use writeToFile:atomically:encoding:error: instead.)
So, it's not like you shouldn't use writeToFile: on an NSString. Just don't do so using the simplest version.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.