Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

perrien

macrumors newbie
Original poster
Apr 26, 2008
20
0
I'm writing some very quick, 1 file command line programs (Xcode and Obj-c of course) and am not sure how to include methods. For instance, this is basically what I want to do:

Code:
#import <Foundation/Foundation.h>

-(NSString *) returnHello {
	return @"Hello";
}

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSLog(@"%@", [self returnHello]);
	
    [pool drain];
    return 0;
}

The above errors out with "fatal error: method definition not in @implementation context".

Thanks for any help.

Ashley
 
If you want to write functions that are not part of a class (basically plain C functions) you don't use + or -:
Code:
NSString *getHello() {
  return @"Hello, World!";
}

and you would call it:
Code:
NSLog(@"%@",getHello());

Code in Objective-C can exist outside of a class, unlike Java. self has no meaning outside of a class.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.