0

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

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

I read that a few times now. However, I would like to know, is that a property (haha see what I did? g) of the language, or of the framework? In other words, would @synthesize still work if I don't derive my class from NSObject, and don't tell the compiler to use Foundation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.