• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: @property and messages
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: @property and messages


  • Subject: Re: @property and messages
  • From: Adam P Jenkins <email@hidden>
  • Date: Thu, 28 Feb 2008 11:08:44 -0500


On Feb 28, 2008, at 9:57 AM, Jamie Phelps wrote:

By now, most of you know I'm working through Aaron Hillegass's book. Along the way, I'm trying to utilize Objective-C 2.0 constructs where I am aware of differences. Hillegass is using explicitly declared setters and getters. I am using @property and @synthesize. In his -(void)setString: method, he writes

- (void)setString:(NSString *)c
{
	c = [c copy];
	[string release];
	string = c;
	NSLog(@"The string: %@", string);
	[self setNeedsDisplay:YES];
}

I have declared my property like this

@property (copy) NSString * string;

So, now I can call self.string = newstring; Great. But it seems that if I do this, I'll be writing the last two lines of the explicit setter each time I change the string. For simple projects, this is obviously not a big deal, but I am curious how to deal with this as projects get larger. Repetitive code would eventually be a maintenance nightmare.

Obviously there isn't anything to stop me from writing the explicit setter, but I wanted to check with the list and see if there is a more elegant way to handle this issue.

If you need your property setter method to do something different than the one that @synthesize generates then you need to write it yourself. Having said that, I'm not sure how often you should really find yourself having to explicitly call setNeedsDisplay: in response to setting a property. If you bind a property to a view, then the bindings framework takes care of notifying the view when the property changes. And you can always setup observers for any property as well.


Should I write the setter explicitly and then continue to use the dot syntax for getting?

Regardless of whether you write your own setter or let @synthesize generate it, you can still use the dot syntax when accessing or changing the property. I.e.
self.string = newstring;
will call your setString: method if you wrote one, or call the one @synthesize generated if you didn't write one.


_______________________________________________

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


References: 
 >@property and messages (From: Jamie Phelps <email@hidden>)

  • Prev by Date: Re: Table views with different NSArrayControllers sharing the same data object...
  • Next by Date: Re: Charting API?
  • Previous by thread: @property and messages
  • Next by thread: RTF pasting as plain text in NSTextView in some cases
  • Index(es):
    • Date
    • Thread