Objective-C - Mac NSLog无法正常工作

时间:2014-07-14 00:30:19

标签: objective-c

我是Obejctive-C和Mac的新手。我一直在尝试在线学习并编写了这段简单的代码,但NSLog对我不起作用。

#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

当我运行程序时,输出控制台会消失,如右上角所示。当我通过显式单击输出控制台来显示输出控制台时,我可以看到程序已退出,代码为0(成功输出?)但NSLog未打印。

enter image description here

如果我在做一些菜鸟错误,请告诉我。)

3 个答案:

答案 0 :(得分:1)

test = [test init];

需要:

test = [[Person alloc] init];

你总是需要分配然后init。您需要指定类名

答案 1 :(得分:1)

你需要两个写:

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

答案 2 :(得分:0)

您需要此代码。

Person *test = [Person new ];