I understand the whole business around reference counting and “owning an object” and that if you allocate an object in Objective-c, it’s your responsibility to release it
However when exactly would you need to call alloc on a newly created object? Would it only be to retain the reference after the end of the scope or is there some other reason
You need to call alloc in order to allocate the memory for the object.
The typical setup of an object is something like:
The alloc call allocates memory for the object, and the init call initialises it (gives it sensible default values for all attributes/properties).
Some object types come with factory methods, eg
In this case, the object is initialised and allocated by the single array call.
None of this has anything (directly) to do with reference counting, except that different ownership rules normally apply to the two methods.