Re: Newbie Question on "self"
Re: Newbie Question on "self"
- Subject: Re: Newbie Question on "self"
- From: Todd Heberlein <email@hidden>
- Date: Mon, 11 Aug 2008 11:17:25 -0700
- (id)initWithString:(NSString *) string {
self.myString = [[NSString alloc] initWithString:string];
}
Well, you don't need the "self" prefix, but you may want to look at
using "setters" and "getters". It would look like this
@interface MyThingy : NSObject {
...
NSString* myString;
...
};
...
@property (copy) NSString* myString;
...
@end
And then in the implementation file you would have something like this:
@implementation MyThingy
...
@synthesize myString;
...
@end
And then, when you set the value, you do it like this:
[self setMyString: anotherString];
The benefit of this approach is that Apple has a run-time system that
monitors access to variables set via setters like this, and other
objects can subscribe to receive notifications when the value has
changed. In particular, you can use Apple's Controller objects to glue
your model (i.e., MyThingy) to GUI elements (e.g., a label in a
window). Then whenever you set myString with the setMyString method,
then the GUI is automatically updated. Likewise, an update in a GUI
element will automatically get pushed into your variable.
All very cool (although a bit confusing when you are first getting
started).
Todd
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden