spandana
spandana

Reputation: 755

unrecognized selector sent to instance 0x10010c730->Objective C

I am new to programming.I have seen this code.returning a derived class object to the base class. So that the base class can then point to the derived class methods. Here a static function in class B is returning its object to the base class.

base-derivedclass.m

#import <Foundation/Foundation.h>
#import "B.h"
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [B p]; 
    [pool drain];
    return 0;
}

A.h

#import <Foundation/Foundation.h>


@interface A : NSObject {

}
@end

A.m

#import "A.h"

@implementation A
 @end

B.h

#import <Foundation/Foundation.h>
#import "A.h"

@interface B : A {

}
+(A*)p;
-(void)display;
@end

B.m

#import "B.h"


@implementation B
+(A*)p
{
    NSLog(@"returning derived class object to the base class!!");
    return [B new];
}
-(void)display
{
    NSLog(@"Hello");
}


@end

Upvotes: 0

Views: 630

Answers (2)

Wienke
Wienke

Reputation: 3733

There's also some confusion in your memory management, here:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
B* der = [[B alloc]init]; 
[der p];
[pool drain];

You create and drain autorelease pools in order to put things into them (after creation) and then dispose of them right away (as you drain them). But you haven't put anything into the pool. You've used "alloc" to create "der," which means you "own" it for memory management purposes, as opposed to its being put in an autorelease pool where it will be taken care of automatically.

If all I've done is confuse you more, you should probably check out some introductory book on Objective-C. They all cover this topic at some point. Or you could look at Apple's docs on memory management, but they assume you already know certain things. (And it IS confusing, so be patient...)

Upvotes: 1

taskinoor
taskinoor

Reputation: 46037

p is a class method. In Obj-C you denote class method by using + in method declaration and - to denote instance method. You can call the class method by using this:

// [ClassName methodName];
[B p];

Or you can change p to instance method by this:

- (A *)p;
// and call
// [instanceName methodName];
[dep p];

You can check Objective-C: A Primer to get started with these.

Upvotes: 2

Related Questions