我正在尝试将布尔值写入plist文件。我的代码是:
NSString *filePath = [self dataFilePath];
if (filePath)
{
if (check)
{
NSLog(@"Clear");
[item setObject:[NSNumber numberWithBool:NO] forKey:@"check"];
[item writeToFile:filePath atomically: YES];
}else{
NSLog(@"Green");
[item setObject:[NSNumber numberWithBool:YES] forKey:@"check"];
[item writeToFile:filePath atomically: YES];
}
NSLog([[NSString alloc] initWithContentsOfFile:filePath]);
}else{
NSLog(@"write file was not found");
}
我的文件路径是:
- (NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"CheckMark.plist"];
}
我用:
加载我的数组- (void)viewDidLoad
{
NSString *filePath = [self dataFilePath];
NSLog(@"%@", filePath);
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"CheckMark" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:myPathInfo toPath:filePath error:NULL];
}
// load our data from a plist file inside our app bundle
self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
NSLog([[NSString alloc] initWithContentsOfFile:filePath]);
NSLog(@"%@", filePath);
NSLog(@"read dataArray");
每件事情都很好,除非我查看plist文件,没有任何变化。 我正在将plist文件写入我的Documents目录。
感谢你给我的任何帮助。
答案 0 :(得分:0)
请记住,您需要使用NSMutableDictionary
才能设置值。