Property list invalid for format

Discussion in 'iOS Programming' started by iphonejudy, Jan 21, 2010.

  1. iphonejudy macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #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];
        }
    	}
     
  2. xsmasher macrumors regular

    xsmasher

    Joined:
    Jul 18, 2008
    #2
    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];
    }
    
     

Share This Page