About simple type KVO (iOS 5) (non-nsobject)

I ran into the problem of making IOS(objective-c)KVO work for int type keys.

My class declares an int type The property sampleValue. Since int does not automatically implement the KVO function, I have automatically covered this method, so I will cover NototOotifiesObserversforKey as:

+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *) theKey {

BOOL automatic = NO;
if ([theKey isEqualToString:@"sampleValue"]) {
automatic = NO;
} else {
automatic=[super automaticallyNotifiesObserversForKey:theKey];
}
return automatic;
}

Call the method as I expected. I also implement it for the sampleValue property A setter method, as follows:

- (void) setSampleValue:(int)newSampleValue
{
[self willChangeValueForKey:@"sampleValue" ];
sampleValue = newSampleValue;
[self didChangeValueForKey:@"sampleValue"];
}

Set the observer in the observer class like this (dc is The instance of the observed object):

[dc addObserver:self forKeyPath:@"sampleValue" options:NSKeyValueObservingOptionNew context:NULL];

However, update sampleValue will not report to my obse The rver object sends notifications. It is great to update another attribute of the NSDate type.

Can anyone help me figure out what I am doing wrong or what I should do to make this work.

Best regards
Tomo

Maybe I’m in your question Something is missing, but you can easily observe the properties of the int type like other types without doing anything special.

Try deleting your automaticNotifiesObserversForKey: override and your -setSampleValue: setter, just synthesize the sampleValue accessor:

@synthesize sampleValue;

int is the type of value corresponding to key @”sampleValue”, but It is not something that is observed. The object to be observed is dc, and when the sampleValue property changes, it will be responsible for sending the correct notification.

I have encountered making IOS (Objective-c) The problem that KVO works for keys of type int.

My class declares an attribute sampleValue of type int. Since int does not automatically implement the KVO function, I have automatically covered it. This method is used, so the NototOotifiesObserversforKey is overwritten as:

+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)theKey {

BOOL automatic = NO;< br /> if ([theKey isEqualToString:@"sampleValue"]) {
automatic = NO;
} else {
automatic=[super automaticallyNotifiesObserversForKey:theKey];
}< br /> return automatic;
}

The method is called as I expected. I am also sampl The eValue property implements a setter method, as shown below:

- (void) setSampleValue:(int)newSampleValue
{
[self willChangeValueForKey:@ "sampleValue"];
sampleValue = newSampleValue;
[self didChangeValueForKey:@"sampleValue"];
}

Set the observer in the observer class like this (dc is an instance of the observed object):

[dc addObserver:self forKeyPath:@"sampleValue" options:NSKeyValueObservingOptionNew context:NULL];

However, when I update the sampleValue, no notification is sent to my observer object. It is very good to update another attribute of the NSDate type.

Can anyone help me figure out what I am doing wrong or what I should do What makes this work.

Best wishes
Tomo

Maybe I missed it in your question Some things, but you can easily observe the properties of type int like other types without doing anything special.

Try to delete your automaticNotifiesObserversForKey: override and your -setSampleValue: setter , Just synthesize the accessor of sampleValue:

@synthesize sampleValue;

int is the type of value corresponding to key @”sampleValue”, but it It is not something that is observed. The object to be observed is dc. When the sampleValue property changes, it will be responsible for sending the correct notification.

Leave a Comment

Your email address will not be published.