我正在尝试写一个包含字典数组的plist。每个字典都包含一个字符串,用于定义该字典(房间)的属性。我只想写一个String,根据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];
}
}
非常感谢任何帮助。有类似的问题,但没有解决我正在尝试做的事情。感谢。
答案 0 :(得分:0)
// 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];
}
}