ping-ponging between assign and retain in Cocoa programming for Mac and iOS

OK. ARC. It’s great! When it was announced sometime last year I didn’t understand why Apple kept two things which did the same thing, Garbage Collector and ARC. Surely, the good and old retain-release memory management, which was once called garbage collection ( in ’80s ) and psuedo garbage collection ( in mid or late 90’s and 2000s ), is still here to stay, because for people who really cares about the best time to release unused allocated memory anymore, it is the best. (Unlike many people think, this manual memory management is not difficult at all. But I understand why some people think it is difficult. There are a few cases. )

Now, the GC ( Garbage Collector, from now on ) is gone. Please use the better and younger sibling, ARC.

However, I would like to mention something changed in memory management in Cocoa, which made many of us confused. (it’s different from the one mentioned above. ) When there was no iPhone, we wondered why the top-most object should have “assign” in a property declaration. ( Before the @property was introduced, it was “autoreleased”, so we didn’t care, if my brain still remembers it correctly. )

And then, iPhone OS was introduced and it also brought us confusion.
Unlike that for Mac, it was “retain” on the iOS. Well, in Apple’s document, it is explained why, but do you actually remember it? Once you understand it, next time when you go back and forth between Mac and iOS projects, wasn’t it confusing to which rationale is applied to which platform?

Expected memory management qualifier for new iOS dev. and old Mac vs. iOS
Because seeing a picture is better than saying 100 words

Ah… it was a disaster, at least to me, if not to you.

Practically I finished a C# based Windows project recently, although there can be some bugs I need to fix. So, I am back to Mac/iOS, and followed “My First iOS App” document to see what have been changed so far. Well, I kept updated my knowledge of Mac/iOS while I had been working with C# on Windows, it was not that intense, and there can be small but important things I may have missed. So, I found some, which were not even documented ( or I didn’t find a document for it. ) While I was surprising from that because I doubted if it was really necessary ( hint : when you try to connect an outlet or action to a source code – wow! Apple people got rid of introducing member variables in a selected class in IB part of Xcode 4 -, it presents a list like “touch down”, “touch up” etc. ), I found out that they put “weak” for all declarations of IBOutlets. So, semantically, now it is consistent between Mac and iOS. The intention of using assign on Mac is similar to that of using weak reference in GC/ARC.

So, you can think of the history of this things like this :

Mac : assign => assign/weak

iOS : retain => weak

 

Leave a comment