iphone - 尝试将 .plist 中的 BOOL 值存储到实现文件中的 BOOL 变量时崩溃

标签 iphone objective-c ios plist

我在应用程序启动时使用以下代码 (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 值时,应用程序崩溃了。我不太明白问题出在哪里。也许我错过了更大的图景或者 bool 值的一个简单但重要的错误。

如果我删除上面的代码,一切正常。但是如果我把它留在那里,应用程序会崩溃并显示以下消息:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean length]: unrecognized selector sent to instance 0x3f39a9f0'

非常感谢您的帮助!

最佳答案

如果你看这一行

fadeOut = [[NSString stringWithFormat:[dictionary objectForKey:@"Fade out"]]boolValue]

您正在从 NSNumber 初始化字符串(您将 BOOL 存储在 NSNumber 中)。这本身是无效的。

如果您需要 NSNumber 的 bool 值,只需对其使用 boolValue。

fadeOut = [[dictionary objectForKey:@"Fade out"] boolValue];

在您检索 bool 值的所有时间都是如此。

关于iphone - 尝试将 .plist 中的 BOOL 值存储到实现文件中的 BOOL 变量时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8497086/

相关文章:

iphone - 在 Objective C 中创建自定义对象数组

ios - Eureka 表单中的必填字段

iphone - 剪贴板在 iOS 上的官方名称是 "pasteboard"吗?

iphone - iOS 应用程序上的 OpenFeint 集成

iphone - 如何在使用 Storyboard 时在所有屏幕上以单一引用显示 iAd?

ios - 尝试在其 View 不在窗口层次结构中的 ViewController 上呈现

ios - 如何缓存 AVPlayer 视频?

ios - 使用 xib 为所有 iPhone 尺寸调整 UI 的最简单方法

iOS UIImage 如何以编程方式将黑色转换为透明?

ios - 接收 PushKit VoIP 通知 iOS