0

Possible Duplicate:
How to write data in plist?

hi i am now in iphone programming. i want ti add data and save it in plist ....-

i am using following code...

(void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"i amin did load");
    temp =[self readPlist:data];
    NSLog(@"dict data is %@",temp);
    [self writeToPlist];
    temp =[self readPlist:data];
    NSLog(@"dict data is %@",temp);

}
    //
//  NSError *error;
//  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
//  NSString *documentsDirectory = [paths objectAtIndex:0]; //2
//  NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; //3
//  
//  NSFileManager *fileManager = [NSFileManager defaultManager];
//  
//  if (![fileManager fileExistsAtPath: path]) //4
//  {
//      NSString *bundle = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; //5
//      
//      [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
//  }
//  NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
//  
//  NSLog(@"dictionary value is %@",savedStock);
//  
//  value = [[savedStock objectForKey:@"india"] intValue];
//  NSLog(@"value in plist is %d",value);
//  [savedStock release];

- (NSDictionary *)readPlist:(NSString *)fileName 
{  NSLog(@"i am in readlist method");
    NSLog(@"file paased is %@",fileName);
    //NSData *plistData;  
//  NSString *error;  
//  NSPropertyListFormat format;  
//  NSDictionary *plist;  

    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];  
    NSMutableDictionary *plistDict=[[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    return plistDict;

}

- (void)writeToPlist
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

    [plistDict   setValue:@"karachi" forKey:@"pakistan"];
    [plistDict setObject:@"lahore" forKey:@"amerika"];
        [plistDict setObject:@"jabalpur" forKey:@"mp"];

    [plistDict writeToFile:filePath atomically: YES];

    /* This would change the firmware version in the plist to 1.1.1 by initing the NSDictionary with the plist, then changing the value of the string in the key "ProductVersion" to what you specified */
}
Community
  • 1
  • 1
skyblue
  • 1
  • 3
  • Possible duplicates, [http://stackoverflow.com/questions/905542/how-to-write-data-in-plist](http://stackoverflow.com/questions/905542/how-to-write-data-in-plist) You can also try this useful link [click here](http://ipgames.wordpress.com/tutorials/writeread-data-to-plist-file/) – Mobile Developer iOS Android Jul 08 '11 at 13:05

1 Answers1

0

When you call writeToFile:atomically you don't need to give the full path, just the file name. It will automatically put it in the documents directory.

Basically, everywhere where you have written the variable filePath just write @"whatever.plist".

Greg
  • 9,068
  • 6
  • 49
  • 91