0

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.

Dinesh Raja
  • 8,501
  • 5
  • 42
  • 81
  • That is impossible!!! always some initializers are created. Either by init, new, calloc, malloc. – Anoop Vaidya Dec 28 '12 at 19:32
  • This question is basically a duplicate of http://stackoverflow.com/questions/5423211/difference-between-nsmutablearray-array-vs-nsmutablearray-alloc-init – Peter M Dec 28 '12 at 19:47
  • @PeterM I think the question you mentioned above is about memory management and ARC. – Dinesh Raja Dec 28 '12 at 19:51
  • @R.A The reason I mentioned that question is because it discusses the difference between the two styles you mentioned - which seems to me is what you are actually asking, and as is also the answer you selected. – Peter M Dec 28 '12 at 20:03

4 Answers4

2

In your SomeObject class add this class method

+(SomeObject)object{
 SomeObject *object = [[self alloc] init];
 //You can do something else to your object here.
 return object;
}

then elsewhere in your project you can just import SomeObject and do:

SomeObject *myObject = [SomeObject object];

Basically that is all NSArray is doing. If you want your own NSObject to do something like this add class method (+) that returns an instance of it's self. within that method just alloc init, do what ever else you want to that object then return it.

mkral
  • 4,065
  • 4
  • 28
  • 53
  • Sure but to create this array you just call `[NSArray array];` with no explicit init which is what he's referring to. How can you initialize something without initializing it? – mkral Dec 28 '12 at 19:32
  • NO Way to create any object without any initializer. – Anoop Vaidya Dec 28 '12 at 19:33
  • you forget it is also autoreleased ;) – Totumus Maximus Dec 28 '12 at 19:36
  • @AnoopVaidya lol, I know... I wasn't asking the question I was saying you have to – mkral Dec 28 '12 at 19:36
  • Guys.. thanks for the answers. I saw the documentation .. [NSMutableArray array]; shows create a new array and returns.. But initWithArray: method specifies "Initializes a newly allocated array by placing in it the objects contained in a given array." So I think like array: method is not initializing.. Any idea?? – Dinesh Raja Dec 28 '12 at 19:42
  • as long as you use `self = [super init];` you can define your object's init what ever you'd like i.e. `initThisReallyCoolObject` then in that method you need `+(id)initThisReallyCoolObject{ self = [super init] if(self){ //Do Something} return self;}` – mkral Dec 28 '12 at 19:46
  • But based on your question you're talking my first answer. This way you still have to alloc i.e. `SomeObject *object = [[SomeObject alloc] initThisReallyCoolObject];` – mkral Dec 28 '12 at 19:49
2

From Apples documentation Object Initialization you can infer that inititalization is not actually required when creating an object:

Initialization sets the instance variables of an object to reasonable and useful initial values

If your code does not expect initialized instance vars then technically you don't have to init. However there are so many caveats (see the link) that in practice doing an init is what you should be doing. The only exception I can think of is that you have explicitly created the object class from scratch, and not based it off something like NSObject - then you know what state everything is in.

Peter M
  • 7,309
  • 3
  • 50
  • 91
  • Guys.. thanks for the answers. I saw the documentation .. [NSMutableArray array]; shows create a new array and returns.. But initWithArray: method specifies "Initializes a newly allocated array by placing in it the objects contained in a given array." So I think like array: method is not initializing.. Any idea?? – Dinesh Raja Dec 28 '12 at 19:44
1

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.

rooster117
  • 5,502
  • 1
  • 21
  • 19
  • Technically, it's a *class method*; there is no such thing as a static method in Objective-C. Also, `alloc` and `init` are not keywords -- they're method names. – jlehr Dec 28 '12 at 23:38
  • Great, and while you're at it, `alloc`, `init`, `copy`, and `new` are neither functions nor keywords -- they're *methods*. – jlehr Dec 29 '12 at 04:41
1

You have to allocate memory for the object somewhere. In the case of [NSMutableArray array] it is calling +(id) array which would look something like this:

+(id)array
{
  return [[NSMutableArray alloc] init];
}

It has to allocate the memory and initialize the object somewhere, but you can hide the init in a class method like what is being done in the example above.

Barlow Tucker
  • 6,229
  • 3
  • 36
  • 42