Reputation: 1389
I've got an observer on my textFields which looks to see if the "enabled@ property has changed.
(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *) context;
{
UITextField *txtField = (UITextField *)object;
BOOL new = [[change objectForKey:NSKeyValueChangeNewKey] boolValue];
BOOL old = [[change objectForKey:NSKeyValueChangeOldKey] boolValue];
if ((new != old) && (new = YES))
{
[self fadeDisable:txtField];
}
else if ((new != old) && (new = NO))
{
[self fadeEnable:txtField];
}
I thought if I used int new and int old, the 1 or 0 which defined if the property is enabled or not would be returned but when I use NSLog to see what thy are bringing back, it's a long string of numbers.
I looked through the documentation and it seems that objectForKey actually return an id not an integer but i'm not sure what to do.
Edit: i've added the code for my comparison which is trying to determine if it went from disabled to enabled (or vice versa). Also added the boolValue correction as recommended.
It does not give the intended result and doesn't call the correct method. Is it correct? Thanks
Upvotes: 0
Views: 91
Reputation: 7944
NSDictionary
contains objects (like NSNumber
), not primitive types (like int
). As you have noticed,
[change objectForKey:NSKeyValueChangeNewKey]
returns id
. If you want to convert it to int, use
int new = [[change objectForKey:NSKeyValueChangeNewKey] intValue]
Or if the property is a BOOL, even better:
BOOL new = [[change objectForKey:NSKeyValueChangeNewKey] boolValue]
This line of code
int new = [change objectForKey:NSKeyValueChangeNewKey]
results in storing value of pointer to the NSNumber
object into new
integer, this is the "long string of numbers" you mentioned. Strangely, it does compile without even a warning.
Upvotes: 1