0

if header file declares

@interface SomeClass: NSObject {
  Data* d;
}

@property (nonatomic, retain) Data* d;

Why is the following line in the implementation file giving me a warning (and init method does not get called?)

[[[self d] alloc] init];

The warning i get is

Instance method '-alloc' not found (return type defaults to 'id')

Meanwhile, Data has - (id) init method, that is not being called.

Please help me understand why.

3 Answers 3

5

alloc should be invoked on a class, not on an instance.

interface SomeClass : NSObject
{
    Data *d;
}

Declare an init method on SomeClass and make it look like:

- (id) init
{
    self = [super init]; 
    if (self)
    {
        d = [[Data alloc] init];
    }

    return self;
}

- (void) dealloc
{
    [d release];
    [super dealloc];
}  

Now you do:

SomeClass *c = [[SomeClass init] alloc];

And you can use the class. Note that you should probably read a little more on classes and objects and about memory management too (when you should release c, etc.).

If, by any chance, you have the possibility to use ARC (automatic reference counting), you won't need to take care of releasing stuff. But that doesn't come with Xcode 4.1, only with 4.2 which is not publicly accessible, apparently.

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

2 Comments

Rudy - why is [self d] an instance of a class?
Because you declared it as such: d is a pointer to an instance of type Data, and [self d] is a reference to that. d is not the class Data. alloc is a class method, and should be called on a class, like Data, not on an instance, like d.
2

The problem isn't -(id)init, it's -(id)alloc. alloc is a class method of NSObject, which means you send it to the class itself and not to an instance of that class, i.e.:

[Data alloc]; // Correct
[someDataInstance alloc]; // Method not found

When you call [self d], you're given an instance of a Data, which you're then sending a -(id)alloc message to. Since NSObject doesn't have a -(id)alloc (only a +(id)alloc), you get the warning.

6 Comments

(Strictly speaking, if you do [self d] before d has been initialized, you don't get an instance, but that's just nitpicking that delayed me posting an answer because I was trying to figure out how to express it succinctly. :)
@Josh - I was thinking the same thing. If d is a pointer to Data, [self d] should return me a pointer to a class which i then initialize.
@Josh, correct, you get nil, which just quietly fails, and is why he isn't crashing with an unrecognized selector :) I figured it was a distraction from the main point.
Guys - why in this example [self d] is an instance of a class and not a pointer to a class?
@mac, with this terminology, a pointer to Data is synonymous to an instance of Data. It's tempting to think of this is C-like terms, where you would take a pointer, then malloc some memory for it. In Objective-C, the actual mallocing is handled internally by the class that you are allocating. Thus, you don't allocate a pointer to an object directly, you ask the class to do that for you.
|
1

You should be doing

self.d = [[Data alloc] init];

As Matt says, alloc is a class method, and must be called on the class itself.

Comments

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.