我试图让这段代码工作,但似乎无法获得任何显示变量设置的输出:
#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;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
mass of mythingy = 0.000000 kg
time of mythingy = 0.000000 sec
Run Code Online (Sandbox Code Playgroud)
我也尝试不使用@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];
Run Code Online (Sandbox Code Playgroud)
更新:
好吧所以我采取了以前的代码,并添加了一行,它现在看起来像这样,但是令人沮丧,因为我想使用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];
Run Code Online (Sandbox Code Playgroud)
输出此代码:
mass of mythingy = 75.000000 kg
time of mythingy = 5.000000 sec
Run Code Online (Sandbox Code Playgroud)
您必须先创建一个对象(mythingy)才能使用它.尝试更改thingy *mythingy;
为thingy *mythingy = [mythingy new];
或使用自定义方法初始化它(如果已实现).