我在这里遗漏了一些东西,我知道这并不难,但我没有得到它。我有一个字典列表。它被加载到一个可变数组中,然后填充我的 tableView。
plist 示例:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>title</key>
<string>entry1</string>
<key>checkedState</key>
<string>NO</string>
</dict>
<dict>
<key>title</key>
<string>accessoriesImage</string>
<key>checkedState</key>
<string>NO</string>
</dict>
<dict>
<key>title</key>
<string>activationCharge</string>
<key>checkedState</key>
<string>NO</string>
</dict>
我正在尝试访问这些字典值,但我很困惑。
这是我的初始化:
static NSString *kTitleKey = @"title";
static NSString *kCheckedState = @"checkState";
@interface ViewController ()
@end
@implementation ViewController
@synthesize attributesTableView, attributesArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
NSString *attributesFile = [[NSBundle mainBundle] pathForResource:@"attributesPlist"ofType:@"plist"];
attributesArray = [[NSMutableArray alloc] initWithContentsOfFile:attributesFile];
}
return self;
}
跳到相关位,我正在填充我的 tableView 单元格:
cell.textLabel.text = [[attributesArray objectAtIndex:[indexPath row]] valueForKey:kTitleKey];
后来在 didSelectRowAtIndexPath 中,我试图检查那些字典值,这就是我失败的地方。
例子:
if ([[attributesArray valueForKey:kTitleKey = @"All"] valueForKey:kCheckedState = @"YES"]) {
[attributesArray setValue:kCheckedState = @"NO" forKey:@"All"];
[attributesArray setValue:kCheckedState = @"YES" forKey:[[attributesArray objectAtIndex:[indexPath row]] valueForKey:kTitleKey]];
我收到此错误:valueForUndefinedKey:]:此类不符合键 YES 的键值编码。
我知道我只是错误地访问它,但我不确定当我使用一个充满可变字典的可变数组时我是如何做到的。