2011-07-08 47 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"

相关问题