By the question I off course don’t mean “why the hell they did not make garbage collection available for cocoa touch too”.Rather, I want to understand how it happens that Cocoa Touch does not have GC while Objective-C does since the release of 2.0. Doesn’t this depend on the language alone?
Share
You aren’t really asking why Garbage Collection isn’t available on iOS. Rather, you are asking how garbage collection can sometimes be available in a language and sometimes not.
On one level, the answer is simple. Apple simply didn’t include the garbage collected code in the runtime for iOS. Basically, there is code that runs on the Mac that implements the garbage collection. This code is “missing” on iOS. The compiler itself is probably somewhat different, but that’s just a matter of having code in the compiler that says:
if(COMPILING_TO_GC)Basically, Apple does it by compiling differently and providing a different runtime.On a more philosophical level, are these really the same language? You can argue that Objective-C for iOS is not the same language as Objective-C for Mac. The lack of garbage collection on iOS makes the semantics of the language significantly different. So you could view it as two different languages which look very similar but have subtley different semantics.
On a practical level, the differences between the languages are alleviated with the introduction of Automatic Reference Counting (ARC). This does the memory management automatically using reference counting. The effect is that iOS has a weak form of garbage collection. As a result, the semantics differ even more subtley.