.plist檔案的讀寫操作
阿新 • • 發佈:2019-01-22
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];