misguided
misguided

Reputation: 3799

Objective-C - Mac NSLog not working

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;
}

enter image description here

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.

enter image description here

Let me know if I am doing some rookie mistake :)

Upvotes: 0

Views: 164

Answers (3)

sonifex
sonifex

Reputation: 280

You need this code.

Person *test = [Person new ];

Upvotes: 0

Enrico Susatyo
Enrico Susatyo

Reputation: 19790

You need two write:

Person *test = [[Person alloc] init];

Upvotes: 1

rmaddy
rmaddy

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

Related Questions