我试图让这段代码工作,但似乎无法获得任何显示变量设置的输出:
#import <Foundation/Foundation.h>
#import "thingy.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
thingy *mythingy;
[mythingy setMass:75.00];
[mythingy setTime:5.00];
NSLog(@"mass of mythingy = %f kg", [mythingy mass]);
NSLog(@"time of mythingy = %f sec", [mythingy time]);
}
return 0;
}
这是我得到的输出:
mass of mythingy = 0.000000 kg
time of mythingy = 0.000000 sec
我也尝试过不使用@autoreleasepool(ARC),代码如下所示,但输出与以前相同:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thingy *mythingy;
[mythingy setMass:75.00];
[mythingy setTime:5.00];
NSLog(@"mass of mythingy = %f kg", [mythingy mass]);
NSLog(@"time of mythingy = %f sec", [mythingy time]);
[mythingy release];
[pool drain];
更新:
好吧所以我采用了之前的代码,并添加了一行,它现在看起来像这样但工作但令人沮丧,因为我想使用ARC !!!!
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
thingy *mythingy = [[thingy alloc]init];
[mythingy setMass:75.00];
[mythingy setTime:5.00];
NSLog(@"mass of mythingy = %f kg", [mythingy mass]);
NSLog(@"time of mythingy = %f sec", [mythingy time]);
[mythingy release];
[pool drain];
此代码的输出:
mass of mythingy = 75.000000 kg
time of mythingy = 5.000000 sec
答案 0 :(得分:5)
您必须先创建一个对象(mythingy)才能使用它。尝试将thingy *mythingy;
更改为thingy *mythingy = [mythingy new];
或使用自定义方法对其进行初始化。
答案 1 :(得分:2)
你在哪里分配东西?
你需要像
mythingy = [[thingy alloc] init];
// or
mythingy = [thingy new];