Re: key value coding sample code
Re: key value coding sample code
- Subject: Re: key value coding sample code
- From: Matthew Lehrian <email@hidden>
- Date: Sat, 7 Dec 2002 21:44:14 -0500
Hi,
There's one additional nuance to key-value coding that is very handy in
the appropriate situation. valueForKey: and takeValue:forKey: actually
check for an accessor or mutator method (as you described). If they
can't find the appropriate accessor or mutator, they will return or set
the value of a matching public iVar.
In other words, if, in your accessor x, all you do is return the iVar x
(as is the case below), you may omit the accessor altogether and
valueForKey: will still "do the right thing".
So, in summary, valueForKey: and takeValue:forKey: actually do this:
1) Check for appropriate accessor or mutator and call that method
2) If not found, check for appropriate iVar and set or return that
value
This is very, VERY cool if you are doing something like object
persistence and have a bunch of data objects with many iVars
representing your persistent data because you don't have to maintain
accessors or mutators if no additional functionality is required.
(BTW, I'm not saying omitting accessors and mutators or direct
manipulation of iVars are "good things," I'm saying that it's cool that
you don't have to write and maintain them if you don't need to.)
BTW, there's a pretty good description of key-value coding in Aaron
Hillegass' book at the very end of Chapter 5 (pp. 113-115) where some
other details are provided WRT type coercion and some potential
pitfalls with that.
Cheers!
Matthew
On Saturday, December 7, 2002, at 07:57 PM, Buzz Andersen wrote:
>
> I am a complete newbie and I think I should be using Key value Coding
>
> in a programme I have to write and for which I am learning C and
>
> Cocoa.
>
>
You may be making things more complicated than they need to
>
be--key-value coding concept has always been pretty easy to use, IMHO.
>
Here is what you need to do:
>
>
1. In the class that holds your data (let's call it "MyClass"), have
>
accessor methods (like getters and setters in JavaBeans if you're at
>
all familiar with that) to provide outside access to the important
>
data. For example, if your class has two instance variables called X
>
and Y and both are of type NSString, you would need accessor methods
>
like this:
>
>
- (NSString *) x {
>
return x;
>
}
>
>
- (void) setX: (NSString *) newX {
>
[newX retain];
>
[x release];
>
x = newX;
>
}
>
>
- (NSString *) y {
>
return y;
>
}
>
>
- (void) setY: (int) newY {
>
[newY retain];
>
[y release];
>
y = newY;
>
}
>
>
2. When you need to access those values, simply use something like the
>
following:
>
>
- (NSString *) getXValForMyClassInstance: (MyClass *) myClassInstance {
>
return [myClassInstance valueForKey: @"x"];
>
}
>
>
- (NSString *) getYValForMyClassInstance: (MyClass *) myClassInstance {
>
return [myClassInstance valueForKey: @"y"];
>
}
>
>
Does this help at all, or am I missing your problem completely?
>
>
--
>
Buzz Andersen
>
email: email@hidden
>
web: http://www.scifihifi.com
>
_______________________________________________
>
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.
_______________________________________________
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.