QBElementType
@interface QBElementType : NSObject @property NSRange minRange; @property NSRange maxrange;@end@implementation QBElementType @synthesize minRange; @synthesize maxrange;@end
QBElement
@interface QBElement : QBElementType{ QBElementType *_type;} @property (weak) QBElementType *type; @property NSUInteger minA; @property NSUInteger maxA;@end@implementation QBElement @synthesize minA = _minA; @synthesize maxA = _maxA;- (QBElementType*)type{ return _type;}- (voID)setType:(QBElementType*)newType{ [self willChangeValueForKey:@"type"]; _type = newType; // no need to bother with retain/release due to ARC. [self dIDChangeValueForKey:@"type"]; /* Having the following 4 lines commented out or not changes nothing to the error if (!NSLocationInRange(_minA,newType.minRange)) self.minA = newType.minRange.location; if (!NSLocationInRange(_maxA,newType.maxrange)) self.maxA = newType.maxrange.location; */}@end
问题:从那时起,每当我更改Element的Type时,我都会得到一个未被捕获的异常:
Cannot update for observer <NStableBinder…> for the key path
“type.minRange” from <QBElement…>,most likely because the value for
the key “type” was changed without an appropriate KVO notification
being sent. Check KVO-Compliance of the QBElement class.
从KVO-Compliance开始我有什么明显的东西吗?为什么我会收到此错误?
解决方法 您不需要显式调用-willChangeValueForKey:和-dIDChangeValueForKey:因为您的setter命名正确.通过KVC / KVO机器的魔力自动添加对它们的调用.问题可能是他们被有效地被召唤了两次.此外,由于minA和maxA似乎只是从类型派生,因此您可以将它们设为只读并告诉KVO自动通知观察者minA和maxA在任何时候类型更改时都已更改:
@interface QBElement : QBElementType{ QBElementType *_type;} @property (weak) QBElementType *type; @property (Readonly) NSUInteger minA; @property (Readonly) NSUInteger maxA;@end@implementation QBElement+ (NSSet *)keypathsForValuesAffectingValueForKey:(Nsstring *)key{ NSSet *keypaths = [super keypathsForValuesAffectingValueForKey:key]; if ([key isEqualToString:@"minA"] || [key isEqualToString:@"maxA"]) { keypaths = [keypaths setByAddingObject:@"type"]; } return keypaths;}@synthesize type;- (NSUInteger)minA{ return self.type.minRange.location;}- (NSUInteger)maxA{ return self.type.maxrange.location;}@end总结
以上是内存溢出为你收集整理的objective-c – 为什么在实现我自己的KVC setter/getter方法时会出现未捕获的异常全部内容,希望文章能够帮你解决objective-c – 为什么在实现我自己的KVC setter/getter方法时会出现未捕获的异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)