Re: Automatic Key-Value Observing
Re: Automatic Key-Value Observing
- Subject: Re: Automatic Key-Value Observing
- From: James Dempsey <email@hidden>
- Date: Tue, 18 Nov 2003 16:45:23 -0800
Joannou,
>
Correct me if I'm wrong. It seems automatic key-value observing when
>
calling accessor methods only works if your accessor methods take
>
parameters.
KVO should be triggered automatically with accessor methods that follow
the standard Key Value Coding convention. For a 'setter' method, the
method signature is setKey: with some value to be set passed in.
However, the method you describe:
-(void)setName
cannot really be termed an accessor method as the caller is unable to
set a value, nor get one returned.
I am guessing your implementation of the method -setName does some
internal calculation to change the value of an instance variable named
'name'.
You might also consider the following:
1. Changing the name of the method to something like 'calculateName'
to avoid confusion with an accessor method.
2. Create the standard accessor methods -name and -setName:
3. In your 'calculateName' method, calculate the new value and use the
setName: accessor to set it. This will automatically trigger the KVO
notification.
(Alternative)
You can also wrap a change in your object with the following declared
in NSKeyValueObserving.h
- (void)willChangeValueForKey:(NSString *)key;
- (void)didChangeValueForKey:(NSString *)key;
It is probably a better rule of thumb to declare and use accessors
however, as it helps funnel all of your key value observing needs
through accessors rather than having to remember to wrap direct
accesses of an ivar in your class's code.
Hope this helps,
James
On Tuesday, November 18, 2003, at 02:59 PM, Joannou Ng wrote:
>
Hello all,
>
>
Correct me if I'm wrong. It seems automatic key-value observing when
>
calling accessor methods only works if your accessor methods take
>
parameters. For instance, the following when called doesn't invoke
>
automatic key-value observing:
>
>
- (void)setName
>
>
Whereas, the following when called will invoke automatic key-value
>
observing:
>
>
- (void)setName:(NSString *)newName
>
>
I hope I'm wrong.
>
>
Cheers, Joannou.
>
>
There is no energy crisis, food crisis, or environmental crisis. There
>
is only a crisis of ignorance. - R. Buckminster Fuller
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
>
---------------------------
James Dempsey
Operations Engineer
Apple Software Customer Seeding
Apple Software Engineering
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.