KVC on an array in an NSDictionary
KVC on an array in an NSDictionary
- Subject: KVC on an array in an NSDictionary
- From: Scott Ellsworth <email@hidden>
- Date: Tue, 18 Apr 2006 14:38:18 -0700
Hi, all.
I have some code that works, but I do not like it. I am hoping there
is a way to grab/set an element in an NSMutableDictionary held in an
NSDictionary using a keypath, so that views bound to the array will
know something has changed.
Details:
My top level object has an NSDictionary named 'data'. In that
dictionary are a series of NSArrays, named things like
newElementAddedEveryMinuteAtSecond0
newElementAddedEveryMinuteAtSecond1
newElementAddedEveryMinuteAtSecond2
...
newElementAddedEveryMinuteAtSecond59
I have views that watch appropriate arrays via bindings. For example,
[barGraphView bind:@"sourceData" toObject:self
withKeyPath:@"data.newElementAddedeEveryMinuteAtSecond5" options:nil];
Then, at 12:00:05, 12:01:05, 12:02:05 ..., I want to add a new
element on the end of newElementAddedeEveryMinuteAtSecond5. I do
this by:
Assume newValue is the value measured at 12:02:05, for example.
// create new
NSMutableArray * newArray = [NSMutableArray arrayWithCapacity:60];
// copy the old
[newArray insertObjects:oldArray
atIndexes:
[NSIndexSet indexSetWithIndexesInRange:
NSMakeRange(0,58)]];
[newArray addObject:newValue
I then call
[self setValue:newArray
forKey:@"data.newElementAddedeEveryMinuteAtSecond5"]
This works. The array has been replaced by a new one, all the views
get their notifications fired, and everyone is happy.
I am not pleased, though, that I am creating a brand new array every
time through, when all I really want to do is to update the data by
an item.
Is there a KVC-compliant way to remove element zero and add a new
element at the end of an NSMutableArray that will keep KVB/O
notifications working?
Scott
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden