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

iphonejudy

macrumors 6502
Original poster
Sep 12, 2008
301
1
I used the below code for writing datas un the plist.
I got the error"Property list invalid for format".
I cant find the "Jsonapps.plist" under documents folder

Code:
-(void)writeToList
{
	NSLog(@"Usernameeeeeeee=%@",txtUsername.text);
	NSLog(@"Passwordddddddd=%@",txtPassword.text);
	
	//Write data in to plist
	
	NSString *error;
    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Jsonapps.plist"];
	NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
	NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
																   format:NSPropertyListXMLFormat_v1_0
														 errorDescription:&error];
	NSLog(@"plist dict=%@",plistDict);
	NSLog(@"plistpath=%@",plistPath);
	
	if (plistDict) {
		NSLog(@"write file was found");
		[plistDict setValue:txtPassword.text forKey:@"Password"]; 
		[plistDict setValue:txtUsername.text forKey:@"Username"]; 
		[plistDict writeToFile:plistPath atomically: YES];
		
		
	}
    else {
        NSLog(error);
        [error release];
    }
	}
 
That code tries to *read* from a plist first, then set some keys, then write back to the plist. If the plist doesn't exists to begin with then plistDict will be nil and if(plistDict) will be false.

I see you NSLog the value of plistDict - what does that output for you?

You can deal with the nil case by creating a blank dictionary if loading from a file returns nil.

Code:
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
if(!plistDict){
     plistDict = [[NSMutableArray alloc] init];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.