how to create an object using self?

Posted by Nick on Stack Overflow See other posts from Stack Overflow or by Nick
Published on 2010-12-22T22:31:08Z Indexed on 2010/12/22 22:54 UTC
Read the original article Hit count: 176

Filed under:
|

I thought I understood the use of self while referring to anything in the current class. After encountering this warning and subsequent run failure, I have googled many variants of "define self" or "usage of self" and gotten nowhere. This problem is how to create an object without the warning, and understand why.

#import <Cocoa/Cocoa.h>

@interface Foo : NSObject {  
   Foo *obj;  
}  
-(void)beta;  
@end  

#import "Foo.h"  
@implementation Foo  
   -(void)beta{  
   obj = [self new];      // 'Foo' may not respond to '-new'  
}  
@end    

Note, if I substitute Foo for self, there's no problem. I thought the class name and self were equivalent, but obviously the compiler doesn't think so.

  1. Perhaps an explanation of what's wrong here will not only solve my problem but also enlighten my understanding of the usage of self.

  2. Are there any tutorials about proper usage of self? I couldn't find anything beyond something like "self is the receiver of the message," which I didn't help me at all.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about self