IBOutlet on Field or Property?

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I'm currently reading through Beginning iPhone Development: Exploring the iPhone SDK and noticed that they place the IBOutlet declaration on their fields and then add properties that don't have IBOutlet. Yet it most of the Apple samples, they put the IBOutlet on the property instead of the field. Is there any difference between the two or are they two means to the same end?
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #2
I'm not totally sure I understand what you're seeing/saying, but I suspect you're talking about the new "synthesize" stuff with the new Obj-C (I forget what the proper terminology is for it). Yes, they wind up being the same thing.

The point of the synthesis is to cut down on writing code because otherwise you have to write all the accessors yourself. Personally I'm not sure it helps all that much because I think that since you can do it two ways all over the place, it can make the code somewhat confusing, especially to newcomers.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
No, it's not the synthesize stuff. Basically I've seen two ways of declaring IBOutlets in classes. My book always writes it like this:

Code:
@interface Something
{
   IBOutlet UIButton *button;
}

@property (nonatomic, retain) UIButton *button;

@end

But all the stock templates and the Apple sample code moves the IBOutlet over to the property like this:

Code:
@interface Something
{
   UIButton *button;
}

@property (nonatomic, retain) IBOutlet UIButton *button;

@end

I'm just wondering if it matters which has the IBOutlet label on it or if they wind up doing the same thing.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #4
Yeah, actually, that is the "synthesize" stuff I was talking about (again, sorry I don't have the proper terminology, which is no doubt causing confusion).

In the implementation file, those "properties" get synthesized into the same thing as how you'd write it by hand. The terminology is "property", but in reality, they're just instance variables. So: It doesn't matter which way you do it.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #5
Ah ok. Thanks.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #6
FYI: Apple recommends sticking it on the property. I read that in some dev doc that I can't seem to find at the moment.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #7
Josh Wrote:FYI: Apple recommends sticking it on the property. I read that in some dev doc that I can't seem to find at the moment.

Hmm... I wonder why? Maybe it works better with the new IB or something? KVC/KVO should work the same either way, so I'm guessing it must be for the tools' benefit.
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #8
Nick Wrote:I'm currently reading through Beginning iPhone Development: Exploring the iPhone SDK and noticed that they place the IBOutlet declaration on their fields and then add properties that don't have IBOutlet. Yet it most of the Apple samples, they put the IBOutlet on the property instead of the field. Is there any difference between the two or are they two means to the same end?

offtopic: Just one question. I read on amazon that "Beginning iPhone Development: Exploring the iPhone SDK" is a paperback. Does it mean if I buy I will receive a PDF or something like that?

Thanks a lot.
Quote this message in a reply
Moderator
Posts: 3,577
Joined: 2003.06
Post: #9
"Paperback" means it doesn't have a hard cover (like thick cardboard), but instead uses a thick paper (like a card) for the covers. Hard cover books generally cost more. Most computer books I have are paperback. College textbooks are often hard covers.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2009.05
Post: #10
AnotherJake Wrote:Hmm... I wonder why? Maybe it works better with the new IB or something? KVC/KVO should work the same either way, so I'm guessing it must be for the tools' benefit.

The 64-bit runtime can synthesize instance variables using property declarations. Presumably, "IBOutlet" is being moved to the property declarations to make it easier to delete those unneeded instance variable declarations someday.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2009.07
Post: #11
riruilo Wrote:offtopic: Just one question. I read on amazon that "Beginning iPhone Development: Exploring the iPhone SDK" is a paperback. Does it mean if I buy I will receive a PDF or something like that?

Thanks a lot.

Actually you can buy the PDF separately, for $10.
http://www.apress.com/promo/tendollars/
It says you need to answer "a randomly generated question" which I assume means you need the book in hand when you register.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2009.10
Post: #12
Nick Wrote:they place the IBOutlet declaration on their fields and then add properties that don't have IBOutlet. Yet it most of the Apple samples, they put the IBOutlet on the property instead of the field. Is there any difference between the two or are they two means to the same end?

Hi,

in Ye Olde Days™, there were no properties, so one put the IBOutlet label on the ivars. Now that we have properties, you should put them on the property. That way, you can freely choose how to store the property's contents. Put it in an ivar, put it in an ivar with a different name, put several properties into the same internal ivar, put all ivars into a struct to avoid the fragile base class problem, have the New Runtime™ synthesize the ivars for you...

Cheers,
-- Uli
Quote this message in a reply
⌘-R in Chief
Posts: 1,260
Joined: 2002.05
Post: #13
Welcome, Uli!!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Create a dictionary property list programmatically ulquiorra 0 2,190 Jul 14, 2009 05:08 AM
Last Post: ulquiorra
  Save game state using Property list or Text File? Graphic Ace 2 3,642 Apr 6, 2009 03:48 AM
Last Post: Graphic Ace