[英]Writing to a plist containing an Array of Dictionaries
我試圖寫一個包含字典數組的plist。 每個字典都包含一個字符串,用於定義該字典(房間)的屬性。 我只想寫一個字符串,以根據if語句的結果將該屬性設置為某種值。
plist的示例:
客房=(
{
Availability = unavailable;
Floor = 1;
Name = "Ferret Room";
Status = Busy;
Time = "4.00pm";
},
{
Availability = unavailable;
Floor = 1;
Name = "Squirrel Room ";
Status = Busy;
Time = "4.00pm";
},
這是我的代碼示例:
NSString* newPlistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
if ((newPlistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"Favourites/Favourites.plist"]))
{
if ([manager isWritableFileAtPath:plistPath])
{
NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
NSLog(@"infoDict %@", infoDict);
NSLog(@"infoDict %@", [[infoDict objectForKey:@"Availability"] objectAtIndex:0]);
[infoDict setValue:@"available" forKey:@"Rooms.Availability"];
[infoDict writeToFile:newPlistPath atomically:NO];
[manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
}
}
任何幫助將不勝感激。 有類似的問題,但我無法解決。 謝謝。
// Make a path to the plist in the Documents directory (not the bundle directory, because you can't write to that directory)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * newPlistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Favourites.plist"];
NSFileManager* manager = [NSFileManager defaultManager];
if ((newPlistPath)
{
if ([manager isWritableFileAtPath:plistPath])
{
NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
NSLog(@"infoDict %@", infoDict);
NSLog(@"infoDict %@", [[infoDict objectForKey:@"Availability"] objectAtIndex:0]);
[infoDict setValue:@"available" forKey:@"Rooms.Availability"];
[infoDict writeToFile:newPlistPath atomically:NO];
[manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:nil];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.