我在应用程序启动时使用以下代码(MyAppTableViewController)创建了一个.plist:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:finalPath];
if (!fileExists) {
NSLog(@"Creating file");
NSDictionary *plistDict = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"Fade out",@"1",@"Enable Gestures",@"1",@"Proximity Sensor",@"1",@"Keep screen ON",nil];
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistDict) {
[plistData writeToFile:finalPath atomically:YES];
} else {
NSLog(@"Error: %@", error);
[error release];
}
}
这很好用,文件是用键和值创建的。然后我有一个带有四个开关的表,其中一个函数toggle:
作为选择器。
-(void)toggle:(id)sender
内的代码获取开关的值以及indexPath
的行,并相应地修改plist:
-(void)toggle:(id)sender {
UISwitch *aSwitch = (UISwitch *)sender;
UITableViewCell *cell = (UITableViewCell *)aSwitch.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
if (indexPath.section == 0 && indexPath.row == 0) {
[dictionary setObject:[NSNumber numberWithBool:aSwitch.on] forKey:@"Fade out"];
}
//There are further (similar) else if statements, but I'll leave them out.
NSString *error = nil;
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(dictionary) {
[plistData writeToFile:finalPath atomically:YES];
} else {
NSLog(@"Error: %@", error);
[error release];
}
}
这也正常工作并成功修改了plist。当我尝试使用以下代码检索数据时出现问题:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
fadeOut = [[NSString stringWithFormat:[dictionary objectForKey:@"Fade out"]]boolValue];
proximitySensor = [[NSString stringWithFormat:[dictionary objectForKey:@"Proximity Sensor"]]boolValue];
enableGestures = [[NSString stringWithFormat:[dictionary objectForKey:@"Enable Gestures"]]boolValue];
keepScreenOn = [[NSString stringWithFormat:[dictionary objectForKey:@"Keep screen ON"]]boolValue];
在声明文件中,我定义了BOOL fadeOut,proximitySensor,enableGestures,keepScreenOn;
。抓取信息工作正常,但是当我更改BOOL值时,应用程序崩溃了。我不太清楚问题是什么。也许我错过了更大的图片或布尔值的一个简单但重要的错误。
如果删除上面的代码,一切正常。但如果我把它留在那里,应用程序会崩溃并显示以下消息:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean length]: unrecognized selector sent to instance 0x3f39a9f0'
非常感谢帮助!
答案 0 :(得分:1)
如果你看这一行
fadeOut = [[NSString stringWithFormat:[dictionary objectForKey:@"Fade out"]]boolValue]
您正在初始化NSNumber
中的字符串(您在BOOL
中存储了NSNumber
)。这本身就是无效的。
如果您需要NSNumber
的bool值,请在其上使用boolValue。
fadeOut = [[dictionary objectForKey:@"Fade out"] boolValue];
在您检索布尔值的所有时间都是如此。