0

Possible Duplicate:
Class methods which create new instances

I am wondering how to emulate the instantiation of classes like NSString, NSArray and such like this: [NSArray arrayWithObject:object]... in hope of eliminating inits and allocs.

I may not be familiar with what that actually does. According the the Documentation, [NSSArray array] creates and returns an empty array. What does that really mean, any allocations?

I want to be able to have a custom NSObject class and do: [CustomObj customObjWithData:data]

Thanks!

Community
  • 1
  • 1
glesage
  • 955
  • 2
  • 16
  • 31

4 Answers4

5

First write a corresponding custom init... method:

- (id)initWithFoo:(Foo *)aFoo
{
     // Do init stuff.
}

Then add a custom factory method that calls alloc and your custom init... method:

+ (id)customObjWithFoo:(Foo *)aFoo
{
    return [[[self alloc] initWithFoo:aFoo] autorelease];
}

If compiling with ARC, omit the autorelease call.

jlehr
  • 15,557
  • 5
  • 43
  • 45
0

Those are class-methods. Usually, they also have instance init* methods as well. For example...

- (id)initWithData:(NSData*)data
{
  // Call appropriate super class initializer
  if (self = [super init]) {
    // Initialize instance with the data
  }
  return self;
}

+ (id)customObjWithData:(NSData*)data
{
  return [[self alloc] initWithData:data];
}

Now, you can call it...

CustomObj *obj = [CustomObj customObjWithData:data];
Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
  • Unless under ARC, the constructor should send `autorelease` before returning the new object. – jscs Apr 13 '12 at 19:20
  • Yes, but personally, I don't like seeing all the comments about memory management in posts not directly related to that issue. People should know what memory management model they are targeting, and code appropriately. If they already know about memory management, they already know this code assumes ARC. If they don't, then by now they are probably using an ARC release of Xcode. – Jody Hagins Apr 13 '12 at 21:07
-1

You would need to do something like...

+ (CustomObj *)customObjWithData:(NSData *)data {
    return [[[CustomObj alloc] initWithData:data] autorelease];
}

...and implement an initWithData: to handle initialization of variables.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • 3
    Always return `id` from initializers; use `[self alloc]` rather than hard-coding the class name to properly support subclassing. – jscs Apr 13 '12 at 19:19
-1

Al lot of answers here are correct. I self always create a method for easy alloc en instantiation through Storyboard-instantiation:

//FooViewController.m

+ (id)create
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

    return [storyboard instantiateViewControllerWithIdentifier:@"FooViewController"];
}

+ (UINavigationController *)createWithNavagionController
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

    FooViewController *fooViewController = [storyboard instantiateViewControllerWithIdentifier:@"fooViewController"];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:fooViewController];

    return navigationController;
}

And if you need parameter, create class-methods like: createWithName:(NSString *)name

Justin
  • 2,960
  • 2
  • 34
  • 48