Reputation: 3799
I am new to Obejctive-C as well as Mac. I have been trying to learn online and have written this simple piece of code , but the NSLog
doesn't work for me.
#import <Foundation/Foundation.h>
@interface Person : NSObject{
int age ;
int weight;
}
-(void) print;
-(void) setAge:(int) a;
-(void) setWeight:(int) w;
@end
//---implementation---
@implementation Person
-(void) print{
NSLog(@"I am %i years and my weight is %i",age,weight);
}
-(void) setAge:(int) a{
age = a;
}
-(void) setWeight:(int) w{
weight = w;
}
@end
int main(int argc, char *argV[]){
Person *test;
test = [test init];
[test setAge:25];
[test setWeight:75];
[test print];
return 0;
}
When I run the program the output console disappears as shown in the top right hand corner. When I display the output console by explicitly clicking on the same, I can see the program exited with code 0(successful output?) but NSLog
not printed.
Let me know if I am doing some rookie mistake :)
Upvotes: 0
Views: 164
Reputation: 318854
test = [test init];
needs to be:
test = [[Person alloc] init];
You always need to alloc then init. And you need to specify the class name
Upvotes: 1