2011-07-08 70 views
0

可能重複:
How to write data in plist?如何通過iPhone應用程序保存在iphone的plist數據

您好,我現在在iPhone編程。我想TI添加數據,並將其保存在plist中....-

我用下面的代碼...

(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 */ 
} 
+1

請修復格式。 – Greg

+0

可能的重複,[http://stackoverflow.com/questions/905542/how-to-write-data-in-plist](http://stackoverflow.com/questions/905542/how-to-write-data- in-plist)你也可以試試這個有用的鏈接[點擊這裏](http://ipgames.wordpress.com/tutorials/writeread-data-to-plist-file/) –

回答

0

當你調用writeToFile:atomically你不需要給的完整路徑,只是文件名。它會自動將其放置在文檔目錄中。

基本上,你寫的變量filePath的地方寫的只是@"whatever.plist"

相關問題