【问题标题】:MutableArray loaded with a .plist file Boolean Value. How do I test whether True or False?MutableArray 加载了一个 .plist 文件的布尔值。如何测试是真还是假?
【发布时间】:2009-11-08 23:31:59
【问题描述】:

我似乎无法理解如何有条件地测试从 .plist 加载到 mutablearray 的布尔值。我只是不明白我应该做什么并继续收到错误:传递'numberWithBool:'的参数1:'从没有强制转换的指针中生成整数。感谢任何帮助理解这一点!

这是我的代码:

if ([NSNumber numberWithBool:[[self.ListArray objectAtIndex:indexPath.row] valueForKey:@"TrueFalse"]]) {
    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IsSelected.png"]];
}

【问题讨论】:

    标签: boolean nsmutablearray if-statement


    【解决方案1】:

    NSNumber 的方法 numberWithBool: 需要一个布尔值 (BOOL) 作为其参数。

    [[self.ListArray objectAtIndex:indexPath.row] valueForKey:@"TrueFalse"]] 显然返回的不是 BOOL。

    要将 NSNumber 或 NSString 对象转换为布尔值,您可以使用 boolValue 方法。

    所以,请尝试执行以下操作:

    if ([NSNumber numberWithBool:[[[self.ListArray objectAtIndex:indexPath.row] valueForKey:@"TrueFalse"] boolValue]]) {
            cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IsSelected.png"]];
    }
    

    【讨论】:

    • 感谢您的帮助。我知道我想投,但在语法上苦苦挣扎。奇怪的是,我发现当我的字典出于某种原因存储“是”时,我正在用“!”在“IF”语句中进行测试。不是。它有效,但似乎相反。但现在我要接受它。谢谢!
    猜你喜欢
    • 2020-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多