Search This Blog

Monday 30 January 2012

How to Read/Write into Plist

//before starting make sure plist must be in Documents folder not in your resource folder for writting
//Writing into data.plist

    NSArray *arr=[[NSArray alloc]initWithObjects:
                  @"one",@"two",@"three",@"four",@"five",@"six",@"seven",nil];
    NSArray *keys=[[NSArray alloc]initWithObjects:
                   @"11",@"22",@"3",@"4",@"5",@"6",@"7",nil];
    
    NSDictionary *dic =[NSDictionary dictionaryWithObjects:arr forKeys:keys];
    
    NSArray *patharray =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *path =[patharray objectAtIndex:0];
    path =[path stringByAppendingPathComponent:@"/data.plist"];
    
    [dic writeToFile:path atomically:YES];
    
    //Reading from data.plist
    NSDictionary *dic2 =[[NSDictionary alloc]initWithContentsOfFile:path];

    NSLog(@"dic2 :%@",dic2);