Question

I am trying to make a new Objective-C root class on the GNU runtime. Here is what I have so far:

foo.h:

#include <objc/objc.h>

@interface Foo {
  Class isa;
  int bar;
}

+ (id) alloc;
+ (id) newWithBar: (int) bar;
- (id) initWithBar: (int) bar;
- (void) p;
- (void) dispose;

@end

foo.m:

#import <foo.h>
#include <stdio.h>

@implementation Foo

+ (id) alloc {
  return class_createInstance(self, 0);
}

+ (id) newWithBar: (int) bar {
  return [[self alloc] initWithBar: bar];
}

- (id) initWithBar: (int) bar_ {
  bar = bar_;
}

- (void) p {
  printf ("bar=%d\n", self->bar);
}

- (void) dispose {
  object_dispose(self);
}

@end

and a little test program, main.m:

#import <foo.h>

int main(int argc, char *argv[]) {
  Foo *foo = [Foo newWithBar: 3];
  [foo p];
  [foo dispose];
  return 0;
}

When I compile foo.m, I get the following warning:

foo.m: In function ‘+[Foo alloc]’:
foo.m:7:3: warning: return makes pointer from integer without a cast [enabled by default]

Why? When I dive into the header files, I can see that class_createInstance returns id. What am I doing wrong here?

Was it helpful?

Solution

You need to include the header for the objective C runtime. The default behaviour of the compiler is to assume undeclared functions return an int.

#include <objc-auto.h>

Sorry - answer above is for OS X/iOS. For GNU you need to include runtime.h as well as objc.h

#include <objc/runtime.h>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top