Is it possible to observe a readonly property of an object in Cocoa Touch?

天涯浪子 提交于 2019-12-09 02:13:00

问题


I've attempted to observe the (readonly) visibileViewController property of a UINavigationController with no success. I was able to successfully observe a readwrite property I defined myself for testing purposes on another class.

Is it possible to observer readonly attributes?


回答1:


Yes, it is possible to observe read-only properties. However, if the object that declares that property makes changes to the value of that property in a way that is not Key-Value Observing compliant (e.g. changes the value of the instance variable backing that property directly without seding willChangeValueForKey: and didChangeValueForKey: notifications) then observers will not automatically be notified by the KVO system. If you can verify that the value of this property is changing and your observers are not being notified, I would (1) post some code here or elsewhere so that others can help you find your bug and (2) if there is no bug in your code, file a bug on Apple's radar.




回答2:


Yes. An easy way to implement it in your own class is to declare the property as readonly in the .h file and redeclare it as writable in the private interface in the .m file. That way you can synthesize and get the change notifications handled automatically.

In the .h file:

@interface MyClass : NSObject
@property (nonatomic, readonly) BOOL foo;
@end

In the .m file

@interface MyClass ()
@property (nonatomic, readwrite) BOOL foo;
@end

@implementation MyClass

@synthesize foo;

- (void)bar {
    // Observers will see the change
    self.foo = YES;
}

@end



回答3:


You can certainly observe readonly properties but be aware that in order for KVO to work you need to be KVC compliant - which means using either the setter/getter for a property (since you're readonly, you don't get a setter for free via @synthesize) or the property's -setValue:forKey: method.




回答4:


A slightly different answer:

@interface MyClass : NSObject
@property (nonatomic, readonly) BOOL foo;
@end

In the .m file

@interface MyClass ()
@property (nonatomic, readwrite) BOOL foo;
@end

@implementation MyClass

+ (NSSet *)keyPathsForValuesAffectingFoo {
    return [[NSSet alloc] initWithObjects:NSStringFromSelector(@selector(foo)), nil];
}

@end



回答5:


This is definitely be possible using NSKeyValueObserving. Properties actually have getter/setter implementations, they are just done for you by the compiler via the @synthesize keyword in an Objective-C classes implementation. Since the key-value observing protocol is based on the standard getter/setter conventions in Objective-C, observing properties works fine. The documentation (linked above) even mentions class properties by name:

"The NSKeyValueObserving (KVO) informal protocol defines a mechanism that allows objects to be notified of changes to the specified properties of other objects."



来源:https://stackoverflow.com/questions/549672/is-it-possible-to-observe-a-readonly-property-of-an-object-in-cocoa-touch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!