I’m missing something here and I know it’s not that hard but I’m not getting it. I have a plist of dictionaries. It’s being loaded into a mutable array which then populates my tableView.
plist sample:
<?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>
And I’m trying to access those dictionary values but I’m getting confused.
Here’s my initialization:
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;
}
Skipping to the relevant bits, I’m populating my tableView cells with:
cell.textLabel.text = [[attributesArray objectAtIndex:[indexPath row]] valueForKey:kTitleKey];
Later in didSelectRowAtIndexPath I’m trying to check those dictionary values and that’s where I’m failing.
Example:
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]];
I’m getting this error: valueForUndefinedKey:]: this class is not key value coding-compliant for the key YES.’
I know I’m just accessing it wrong but I’m not sure how I do that when I’m using a mutable array filled with mutable dictionaries.
I think your syntax for setting values is off. In the last code snippet you posted, you have this:
Contrary to what you might think, this doesn’t set the title to “All”; instead, it replaces the value of
kTitleKeywith the string “All”, then fetches the value of “All” from the attributes array. Instead, I think you want something like:You’ll have to make similar updates throughout that last example. Remember the critical distinction here:
valueForKey:gets a value. You can never set something usingvalueForKey:, even if you throw some equals signs in.setValue:forKey:sets a value. You’ll want to use this whenever you need to changeattributesArray.You might want to read up on key-value coding for more information.