Re: key value coding sample code
Re: key value coding sample code
- Subject: Re: key value coding sample code
- From: Buzz Andersen <email@hidden>
- Date: Sat, 7 Dec 2002 17:57:50 -0700
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.