I don’t know this question makes any sense. But I wanna know how we can create new instance for an NSObject without using any initializers. I have seen that I can create new NSMutableArray like this two ways.
NSMutableArray *myArray = [[NSMutableArray alloc]init];
Generally I will create an instance for my NSObject like above using init: method or any other designated initializers. But I don’t know how to create a method like below example..
NSMutableArray *myArray = [NSMutableArray array];
I have seen the documentation for NSMutableArray and it is a subclass of NSArray . And also NSArray having a method like this to achieve an above example.
+(id)array;
But I wanna know what could the code inside of the array: method. Otherwise how to write a method same as like that for my own NSObject ?? Before vote down ask me, if you want more explanation.
What you are seeing is a class method on an object. [NSMutableArray array]; is creating an allocated array and returning it to you. Before arc this was important because it would also return to you an autoreleased array. But to answer your question it is behind the scenes calling an alloc/init function at some point.
Also note that “alloc” is the function to allocate the memory while the init is the function that initializes the object once it is allocated. There are other keywords that you can use to allocate memory like “copy” and “new” but I doubt that is what you are looking for.