tags:

views:

52

answers:

2

hi guys, I'm new with objective c for the iphone.

I'm writing a custom class, then, should I write my own +alloc, +init methods? I believe that: the +alloc will just call [thing alloc]; and the +init will perform something like: [thing setValue:X];

is there a "default" +alloc and +init methods? if yes, what should I wait for the default +alloc and +init?, let's say i have a NSMutableDictionary* i.e.

@interface class1 : NSObject {

    NSString *test; 
    NSMutableDictionary *map;
}

thanks

+1  A: 

You should NEVER override alloc.

As for init, which is an instance method, by the way, yes that would be the place where you'd probably want to set up your variables.

Jacob Relkin
+2  A: 

You generally don't need to write your own +alloc method.

For your example, you might have an -init that looks like:

- (id)init {
    self = [super init];
    if (self) {
        map = [[NSMutableDictionary dictionary] retain];
    }
    return self;
}

Note that you first call the superclass -init, and check that it worked, before initializing your own variables.

David Gelhar
great, thanks fo the answer
jhon