PDA

View Full Version : iPhone Development book, unnecessarily setting properties??




CaptSaltyJack
May 1, 2009, 01:23 AM
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:


@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:


@property (retain, nonatomic) UILabel *statusText;


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


[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
May 1, 2009, 03:59 AM
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
May 1, 2009, 04:13 AM
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
May 1, 2009, 09:40 AM
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
May 1, 2009, 10:23 AM
Delete this

CaptSaltyJack
May 1, 2009, 10:24 AM
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:


@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:


nameInput = ...some value...


you're using them like this:


[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.

CocoaPuffs
May 1, 2009, 12:24 PM
You do know the book has its own forum, right? http://www.iphonedevbook.com

CaptSaltyJack
May 1, 2009, 12:29 PM
You do know the book has its own forum, right? http://www.iphonedevbook.com

I do now! Thanks!