我遇到了使IOS(objective-c)KVO为int类型的键工作的问题.
我的类声明了一个int类型的属性sampleValue.由于int没有自动实现KVO功能,所以我已经自动覆盖了该方法,因此将NototOotifiesObserversforKey覆盖为:
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)theKey {
BOOL automatic = NO;
if ([theKey isEqualToString:@"sampleValue"]) {
automatic = NO;
} else {
automatic=[super automaticallyNotifiesObserversForKey:theKey];
}
return automatic;
}
Run Code Online (Sandbox Code Playgroud)
正如我所期望的那样调用该方法.我也为sampleValue属性实现了一个setter方法,如下所示:
- (void) setSampleValue:(int)newSampleValue
{
[self willChangeValueForKey:@"sampleValue"];
sampleValue = newSampleValue;
[self didChangeValueForKey:@"sampleValue"];
}
Run Code Online (Sandbox Code Playgroud)
在观察者类中设置观察者就像这样(dc是被观察对象的实例):
[dc addObserver:self forKeyPath:@"sampleValue" options:NSKeyValueObservingOptionNew context:NULL];
Run Code Online (Sandbox Code Playgroud)
但是,更新sampleValue时,不会向我的observer对象发送通知.更新NSDate类型的另一个属性非常好.
任何人都可以帮助我弄清楚我做错了什么或者我应该做些什么来使这项工作.
最好的问候Tomo
我需要将NSString表示十进制值的s 转换为十六进制字符串,例如,值为"10"的字符串应转换为字符串"A","15"应转换为"E","20"应为"14"等等上.
我怎么做?