16th
IBOutlet syntax
Ever since I started learning iPhone development, I’ve seen all sort of different syntax and one that really got me thinking was IBOutlet, so I fired up an email to Jeff LaMarche:
Hi Jeff, I’ve seen 3 ways people write down the IBOutlet syntax:
1)
IBOutlet Something *something;
@property (nonatomic, retain) IBOutlet Something *something;2)
Something *something;
@property (nonatomic, retain) IBOutlet Something *something;3)
IBOutlet Something *something;
@property (nonatomic, retain) Something *something;and I think I’ve seen a 4th one also:
IBOutlet Something *something;
no property.Is there any difference in the first 3? Is the 4 one valid in some cases?
Thanks, fan of your blog/twitter, Rogelio Gudino.
I was lucky and got an answer from him:
Rogelio:
Actually, 2, 3, and 4 are valid, but #1 is incorrect. #1 (at least the last time I tried it, which was when IB3 came out) will parse into two separate outlets with the same name, leading to confusion.
#4 is the traditional, Objective-C 1.0 way of doing things, and it it still valid. Since the bundle loader will automatically retain the root elements of the nib, and sub elements will be retained by their owner, there’ no problem with not using a property.
#2 is the “preferred” way with Objective-C 2.0.
#3 is the way most developers did it early on with Objective-C 2.0. Then Apple’s sample code and documentation started using #2 and most people switched.
I could add two more to the mix.
5)
Something *something;
@property (nonatomic, assign) IBOutlet Something *something;6)
IBOutlet Something *something;
@property (nonatomic, assign) Something *something;Some developers are saying why bother retaining the outlets when the bundle loader is going to do it for us.
Hope that helps.
Jeff
So if you had the questions I did, I’m pretty sure that makes it all more than clear. I recommend reading Jeff’s blog, where you’ll be able to find more of him: http://iphonedevelopment.blogspot.com/
Tech
Education
Tea
Movies
Other