iPhone Development book, unnecessarily setting properties??

Discussion in 'iOS Programming' started by CaptSaltyJack, Apr 30, 2009.

  1. CaptSaltyJack macrumors 6502

    Jun 28, 2007
    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??
  2. jnic macrumors 6502a

    Oct 24, 2008
    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.
  3. BlackWolf macrumors regular

    Apr 9, 2009
    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
  4. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    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.

  5. CaptSaltyJack thread starter macrumors 6502

    Jun 28, 2007
  6. CaptSaltyJack thread starter macrumors 6502

    Jun 28, 2007
    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:


    See, Apple doesn't use @property for the UITextFields..but they do use it for the UITableView. Weird.
  7. CocoaPuffs macrumors 68010

    Aug 23, 2008
  8. CaptSaltyJack thread starter macrumors 6502

    Jun 28, 2007

Share This Page