Does readonly property for IBOutlet Work and would it be Preferable?

The nib loader on iOS creates the objects in the nib and then autoreleases them. When it establishes connections to outlets, it uses setValue:forKey: which will call the setter method for that key. If no setter is defined, such as when the IBOutlet is a readonly property, the object is retained anyways before being assigned.(This is a paraphrase of Managing Nib Objects in iOS in the Resource Programming Guide.) So in actual fact, whether the outlet is declared as retain or assign the object at the other end is owned by the object with the outlet.

Either it is retained by the setter method, or it is retained by setValue:forKey: when a setter is not found. Since there is no other possible owner in the second case, you may consider the object with the outlet to be the owner. Therefore the object in the nib should be released in dealloc I agree with you that this memory condition should be made explicit by changing the property attributes to include retain Whether or not it's readonly doesn't seem to make a difference (however, see below).

Conceptually, yes, the object is read-only, so whether to mark it explicitly as such depends on whether you consider that suitably documented by the fact that it's an IBOutlet UPDATE: Paul. S's comment below prompted me to do a quick test. I created a UIView subclass that logs its alloc retain release and autorelease calls, stuck an instance of it into a nib, and gave the app delegate a IBOutlet via a property Tallying up the reference counting activity by hand, the instance came out with a net 0 count when the property was (readwrite, assign) It was net +1 when the property was declared the recommended way (readwrite, retain) and also when it was (readonly, assign) All of this is pretty much as expected -- when it is (readwrite, assign) the assigning setter is used to make the connection, and no retain is made.

When it is readonly the connection mechanism falls back on doing its own retain Most interestingly, when I tried to crash the app by changing the background color of this view with the property declared (readwrite, assign) (i.e. , when it had presumably been deallocated), I saw one last call to retain pop up I think what this comes down to is: follow Apple's recommendation -- they know what's going on behind the scenes, and (barring bugs) aren't going to steer you wrong (Another thing to take away is that, as always, worrying about absolute reference counts is not going to be terribly useful -- the count went all the way up to 6 at one point, over the course of two dozen calls to retain and release you just need to worry about retains and releases that you cause directly.) Of course, this changes under ARC. The info I paraphrased is in the "Legacy Patterns" section of its chapter.

Under ARC, the recommendation is for IBOutlets to be weak unless they are top-level, in which case they should be strong Doing it that way means you are relying on the view hierarchy (views retaining their subviews) to maintain itself.

The nib loader on iOS creates the objects in the nib and then autoreleases them. When it establishes connections to outlets, it uses setValue:forKey:, which will call the setter method for that key. If no setter is defined, such as when the IBOutlet is a readonly property, the object is retained anyways before being assigned.(This is a paraphrase of Managing Nib Objects in iOS in the Resource Programming Guide.) So in actual fact, whether the outlet is declared as retain or assign, the object at the other end is owned by the object with the outlet.

Either it is retained by the setter method, or it is retained by setValue:forKey: when a setter is not found. Since there is no other possible owner in the second case, you may consider the object with the outlet to be the owner. Therefore the object in the nib should be released in dealloc.

I agree with you that this memory condition should be made explicit by changing the property attributes to include retain. * Whether or not it's readonly doesn't seem to make a difference (however, see below). Conceptually, yes, the object is read-only, so whether to mark it explicitly as such depends on whether you consider that suitably documented by the fact that it's an IBOutlet.

UPDATE: Paul. S's comment below prompted me to do a quick test. I created a UIView subclass that logs its alloc, retain, release, and autorelease calls, stuck an instance of it into a nib, and gave the app delegate a IBOutlet via a property.

Tallying up the reference counting activity by hand, the instance came out with a net 0 count when the property was (readwrite, assign). It was net +1 when the property was declared the recommended way, (readwrite, retain), and also when it was (readonly, assign). All of this is pretty much as expected -- when it is (readwrite, assign), the assigning setter is used to make the connection, and no retain is made.

When it is readonly, the connection mechanism falls back on doing its own retain. Most interestingly, when I tried to crash the app by changing the background color of this view with the property declared (readwrite, assign) (i.e. , when it had presumably been deallocated), I saw one last call to retain pop up.

I think what this comes down to is: follow Apple's recommendation -- they know what's going on behind the scenes, and (barring bugs) aren't going to steer you wrong.(Another thing to take away is that, as always, worrying about absolute reference counts is not going to be terribly useful -- the count went all the way up to 6 at one point, over the course of two dozen calls to retain and release -- you just need to worry about retains and releases that you cause directly. ) *Of course, this changes under ARC. The info I paraphrased is in the "Legacy Patterns" section of its chapter.

Under ARC, the recommendation is for IBOutlets to be weak unless they are top-level, in which case they should be strong. Doing it that way means you are relying on the view hierarchy (views retaining their subviews) to maintain itself.

I only mention as it's the only part of your answer that was a bit ambiguous – Paul. S Nov 2 at 0:54 I think so...that's certainly what it seems to say. – Josh Caswell Nov 2 at 0:55 2 @Paul.

S: see my update -- I decided to count the reference activity by hand and the results were, I think, instructive. – Josh Caswell Nov 2 at 1:20 Nice bit of curious investigation – Paul. S Nov 2 at 1:24 1 @Sam: I think you should go with Apple's recommendation: (readwrite, nonatomic, retain) (as you say, you can leave out the explicit readwrite).

Meaning no disrespect to your coworker, concision certainly contributes to readability, but sometimes writing what everyone expects is more important. If he had done it the recommended way, you wouldn't have had to spend any time at all puzzling over the implications. While it was an interesting thing to look into, you probably could have gotten (more important) things done in that time.

– Josh Caswell Nov 2 at 19:35.

I have reported a bug with Apple that if you create IBOutlet instance variables instead of properties, that Xcode will still automatically create release's in dealloc etc. Xcode for iOS apps seems to always create releases for IBOutlet whether its correct or not. Personnel I don't like property IBOutlet as it means you declare them as readwrite, which means they are documented as readwrite, but most of the time, (nearly always), IBOutlet should conceptually be readonly. Obviously they have to be readwrite to set initially.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions