tags:

views:

172

answers:

2

I am a objective C newbie. Why do i get a warning for calling a func i created in a category?

#import <stdio.h>
#import <objc/Object.h>

@interface MyObj: NSObject
{
@public
    int  num;
}

-(void) print;
@end

@implementation MyObj;
-(void) print
{
    printf("%d\n", self->num);
}
@end

@implementation MyObj(more)
-(void) quack
{
    printf("Quack\n");
}
@end


int main (int argc, char *argv[])
{
    MyObj  *obj = [[MyObj alloc] init];
    obj->num = 9;
    [obj print];
    [obj quack]; //warning my not respond to quack 
    [obj release]; 
}
+3  A: 

You need to declare the interface for MyObj(more), eg.

@interface MyObj(more)
-(void)quack;
@end
olliej
+1  A: 

Just wanted to point out a couple of things because you mentioned that you're new to Obj-C.

Inside of a method, you don't need to refer to self-> to get at instance variables, so your print method can be:

-(void) print
{
    printf("%d\n", num);
}

Also note that you normally never directly access an instance variable outside of the object's methods (i.e. you wouldn't do obj->num -- in fact you don't often see the -> operator in Obj-C code). Instead you'd specify accessors for the property (assuming this is Objective-C 2.0):

// In the interface:
@property (assign) int num;

// In the implementation:
@synthesize num;

// In main:
obj.num = 9;
Daniel Dickison
thats good to know. It appears i am using 2.0. thanks :). This looks much nicer
acidzombie24