まーぽんって誰がつけたの?

iOS→Scala→インフラなおじさん技術メモ

Objective-Cのallocは何をしてるのか

allocはNSObjectのクラスメソッド

alloc - NSObject | Apple Developer Documentation

+ (instancetype)alloc;

The isa instance variable of the new instance is initialized to a data structure that describes the class; memory for all other instance variables is set to 0.

初期化前にメモリを確保。なのでこの時点で一応インスタンスはできている。でも初期化はされてない。サイズは確保されてない状態ってことだと思う。initして初めてサイズが確保される。なので、allocの返り値の型とinitの返り値の型は同じ。

Swiftの場合、 Hoge() ってやると、[[Hoge alloc] init]; まで呼ばれるのでallocを意識することはない。

allocationの説明

developer.apple.com

isaとは

http://algorithm.com.au/downloads/talks/objective-c-internals/objective-c-internals.pdf

In other words, an NSObject is simply a struct with a single field, named isa (“is a”, as in “a car is a vehicle”) that points to some sort of Class type.