View Full Version : A simple question about assign, copy and retain

Oct 10, 2010, 12:39 AM
I found a explanation for property retain and release with real life example as below, it is very easy to understand.

I want to know if someone can use similar story to explain for copy and assign.

Imagine a room in the library that you can reserve for studying purposes. Initially, the room is empty and hence the lights are off. When you reserve the room, the librarian increases a counter to indicate the number of persons using the room. This is similar to creating an object using the alloc keyword.

When you leave the room, the librarian decreases the counter, and if the counter is now 0, this means that the room is no longer being used and the lights can thus be switched off. This is similar to using the release keyword to release an object.

There may be times when you have booked the room and are the only one in the room (hence, the counter is 1) until a friend of yours comes along. He may simply come and visit you and therefore doesn't register with the librarian. Hence, the coun-ter does not increase. Because he is just visiting you and hasn't booked the room, he has no rights to decide whether the lights should be switched off. This is similar to assigning an object to another variable without using the alloc keyword. In this case, if you leave the room (release), the lights will be switched off and your friend will have to leave.

Consider another situation in which you are using the room and another person also booked the room and shares it with you. In this case, the counter is now 2. If you leave the room, the counter goes down to 1, but the lights are still on because another person is in the room. This situation is similar when you create an object and assign it to another variable that uses the retain keyword. In such a situation, the object is released only when both objects release it.

Continuing with our analogy of the room in the library, imagine that you are about to sign out with the librarian when you realize that you have left your books in the room. You tell the librarian that you are done with the room and want to sign out now, but because you left your books in the room, you tell the librarian not to switch off the lights yet so that you can go back to get the books. At a later time, the librarian can switch off the lights at his or her own choosing. This is the behavior of autoreleased objects.

Oct 11, 2010, 09:38 AM
I think the explanations regarding alloc are seriously inaccurate. Alloc would be more like the librarian reserves some empty space in the library, enough to hold the room. Then, extending the metaphor, doing the init would be like having a construction crew come in and building an actual room in that reserved space. At least, that's my perspective.

Oct 11, 2010, 10:41 AM
Alloc is to buy an air matress; init is to inflate it :D

Oct 11, 2010, 03:00 PM
Hope I'm not confusing matters, but I might try explaining with a different analogy:

Think of a property as the contact number of a colleague you need to work with. You need 'his' number to send messages to him.

Assign: You are given his number, but he may be fired at any point and you will not be notified. If you try to call him using that number after he's been fired, problems will happen. (If you were previously storing another person's number in that slot, it's now gone.)

Retain: You are given his number, but feel safe knowing he cannot be fired until you wipe - i.e. release - his number. When you wipe his number, you have no way of getting in touch with him, and HR is contacted to let them know you no longer need him. If no one needs him, they fire him. (If you were previously storing another person's number in that slot, that person's number is wiped, and they're possibly fired, but you don't care any more). Make sure you wipe his number when he's done, you don't want lots of employees hanging around who aren't doing anything!

Copy: He's likely to be busy working for several people on conflicting jobs, so you make a clone of him! You are given the clone's number, and whatever you tell the clone has no effect on the original colleague. The same wiping rules apply as for retain.

(Apologies for the heartless analogy! Hope it's accurate and helpful though!)