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
#import
@interface MyPoint: NSObject
@property float x;
@property float y;
-(float) distance:(MyPoint*)other;
-(NSString*) description;
@end
@implementation MyPoint
@synthesize x,y;
-(float) distance:(MyPoint*)other{
return sqrt(((self.x-other.x)*(self.x-other.x))+
((self.y-other.y)*(self.y-other.y)));
}
-(NSString*) description{
return [NSString stringWithFormat:@"(%f,%f)",self.x,self.y];
}
@end
int main(int argc,char *argv[]){
MyPoint *a=[[MyPoint alloc] init];
MyPoint *b=[[MyPoint alloc] init];
a.x=10.0f;
a.y=10.0f;
b.x=20.0f;
b.y=20.0f;
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
./point
By: gavi on: