A simple question about assign, copy and retain

Discussion in 'iOS Programming' started by mikezang, Oct 9, 2010.

  1. macrumors 6502a

    May 22, 2010
    Tokyo, Japan
    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.

  2. Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    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.
  3. macrumors 6502

    Jun 22, 2010
    Alloc is to buy an air matress; init is to inflate it :D
  4. macrumors 603


    Aug 2, 2002
    Cork, Ireland.
    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!)

Share This Page