搜尋此網誌

2012年8月29日 星期三

read write plist 存讀取plist


read write plist 存讀取plist

//讀檔

- (void)readPlist
{
//取得檔案路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:@"/data.plist"];
//
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *plistDict;
if ([fileManager fileExistsAtPath: filePath]) //檢查檔案是否存在
{
plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}else{
plistDict = [[NSMutableDictionary alloc] init];
}
NSString *value;
value = [plistDict objectForKey:@"version"];

[plistDict release];

}

//================================================================
//存檔

//存檔
- (void)writePlist
{
//取得檔案路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingString:@"/data.plist"];

//
NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *plistDict;
if ([fileManager fileExistsAtPath: filePath]) //檢查檔案是否存在
{
plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
}else{
plistDict = [[NSMutableDictionary alloc] init];
}


[plistDict setValue:@"1.0.0" forKey:@"version"];

//存檔
if ([plistDict writeToFile:filePath atomically: YES]) {
NSLog(@"writePlist success");
} else {
NSLog(@"writePlist fail");
}
[plistDict release];

}

//================================================================
注意事項
1.未存過的檔案的第一次建構
2.檔案的路徑


沒有留言:

張貼留言