Compiling Objective C on Command Line with Foundation Framework and ARC

I think learning Objective-C on command line gives you better understanding rather than just use XCode generated templates. For Example take the following code. Create a file called point.m and put the following code


@interface MyPoint: NSObject
@property float x;
@property float y;
-(float) distance:(MyPoint*)other;
-(NSString*) description;

@implementation MyPoint
@synthesize x,y;
-(float) distance:(MyPoint*)other{
	return sqrt(((self.x-other.x)*(self.x-other.x))+
-(NSString*) description{
	return [NSString stringWithFormat:@"(%f,%f)",self.x,self.y];

int main(int argc,char *argv[]){
	MyPoint *a=[[MyPoint alloc] init];
	MyPoint *b=[[MyPoint alloc] init];
	NSLog(@"a %@",a);
	NSLog(@"b %@",b);
	NSLog(@"Distance is %f",[a distance:b]);
	return 0;

Now compile it using the command below

clang -fobjc-arc point.m -o point -framework Foundation

Run it below


By: gavi on: