怎么在xcode工程中创建自己的plist文件

转载:

怎么在xcode工程中创建自己的plist文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"]; NSFileManager *fileManager = [NSFileManager defaultManager];



if (![fileManager fileExistsAtPath: path]) 

{

    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"plist.plist"] ];

}



NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];





NSFileManager *fileManager = [NSFileManager defaultManager];

NSMutableDictionary *data;



if ([fileManager fileExistsAtPath: path]) 

{

            data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

}

else

{

    // If the file doesn’t exist, create an empty dictionary

    data = [[NSMutableDictionary alloc] init];

}



//To insert the data into the plist

int value = 5;

[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];

[data writeToFile: path atomically:YES];

[data release];



//To reterive the data from the plist

NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

int value1;

value1 = [[savedStock objectForKey:@"value"] intValue];

NSLog(@"%i",value1);

[savedStock release];
 

posted on 2011-10-27 09:27  wtq  阅读(4404)  评论(0编辑  收藏  举报