Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CaptSaltyJack

macrumors 6502
Original poster
Jun 28, 2007
351
1
For anyone who has the Apress "iPhone Development" book: is it just me, or do they keep overusing @property and @synthesize? For example, for EVERY UI element they declare as an IBOutlet, they also use @property and @synthesize on it. I think this is incorrect. When working with Cocoa on Mac OS, you only use @property for values you're actually going to work with as getters/setters directly:

Code:
@property (readwrite, retain) NSString *carName;
...
@synthesize carName;
...
[someObj setCarName:@"Honda"];

But man, in this iPhone book, they use @property on EVERY little thing. Just as a test, for the "Control Fun" exercise, I commented out all the @property and @synthesize statements for the UI outlets, and the program still built and ran just fine. So what's with the author's obsession of using @property?

The first apparent misuse of this is on page 34, where the author has a UILabel to show the result of a button press. He declares the IBOutlet, then outside the interface declaration:

Code:
@property (retain, nonatomic) UILabel *statusText;

This would be appropriate, I think, if he were going to set it DIRECTLY:

Code:
[myView setStatusText:(UILabel *)someOtherLabel];

But he only calls methods of statusText, e.g. statusText.text = newText.

Can someone clear this up for me? He's basically creating getters & setters (statusText, setStatusText) that are never used, right??
 

jnic

macrumors 6502a
Oct 24, 2008
567
0
Cambridge
Can someone clear this up for me? He's basically creating getters & setters (statusText, setStatusText) that are never used, right??

Correct, looks like the author's gone a bit property-happy. Not sure if that's ignorance on his part, or an attempt to get people using convenience methods, but does seem a little excessive.
 

BlackWolf

macrumors regular
Apr 9, 2009
244
0
well, apple does that as well in their sample codes for iphone coding.

you need that for IBOutlets anyway, because IBOutlets should have a retain-setter. don't see the big deal anyway, at least it's clear what property has what getters and setters
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
Can someone clear this up for me? He's basically creating getters & setters (statusText, setStatusText) that are never used, right??

Yes. It's part of the religion. The OOP sect believes that object state should only ever be touched through messages/methods, so they set up all the getters and setters in advance, so should they ever need to touch that part of object state in the future, they won't be tempted by evil spirits to diddle the encapsulated bits directly (more efficient, but with more possibilities for screwing up), even though Objective-C doesn't enforce that idea.

I sometime do the same just to make the code more parallel looking and readable.

If I hack the object state directly, I do find myself occasionally spending time chasing/debugging forgotten retains, etc. So you either spend the time here, or there.

YMMV.
 

CaptSaltyJack

macrumors 6502
Original poster
Jun 28, 2007
351
1
you need that for IBOutlets anyway, because IBOutlets should have a retain-setter. don't see the big deal anyway, at least it's clear what property has what getters and setters

But you're not actually setting those items directly.. or getting values from them directly (by directly, I mean not using a method). So, for example:

Code:
@interface MyViewController : UIViewController
{
  IBOutlet UITextField *nameInput;
  IBOutlet UITextField *ageInput;
}
- (IBAction)submitCustomerInfo:(id)sender;

That's it. You don't need to use @property on nameInput and ageInput because you are NOT setting/getting them directly, like this:

Code:
nameInput = ...some value...

you're using them like this:

Code:
[nameInput setText:@"Blah blah"]; //sorry, forgot the method to set text in a UITextField

Just found this:

https://developer.apple.com/iPhone/library/samplecode/EditableDetailView/listing9.html

See, Apple doesn't use @property for the UITextFields..but they do use it for the UITableView. Weird.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.