简单的ObjC程序即将冻结

时间:2015-07-29 09:07:17

标签: objective-c

为什么ObjC程序卡住了? 我是ObjC的新手并试图通过命令在Linux系统上编译它:

gcc $(gnustep-config --objc-flags)* .m $(gnustep-config --base-libs)

这里是代码:

int main (int argc, const char * argv[])
{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

        Rect * r = [[Rect alloc]initWithWidth:30 height:50];
        NSLog(@"%f", [r height]);

        [pool drain];
        return 0;
}

//=====
#import <Foundation/Foundation.h>

@interface Rect : NSObject <NSCopying> {
}

@property double width;
@property double height;

- (Rect *) initWithWidth:(double)w
                  height:(double)h;
- (double) height;
//...

//=====
@implementation Rect
- (Rect *) initWithWidth:(double)w
                  height:(double)h
{
    self.width = w;
    self.height = h;
    return [super init];
}

- (double) height
{
    return self.height;
}
// ...

1 个答案:

答案 0 :(得分:1)

当您致电return self.height;将该行更改为return _height;时,您正在创建一个无限递归循环,它应该可以正常工作。您还可以从标头文件中删除- (double) height;,因为您的属性声明会自动生成一个setter - (void)setHeight:(double)height;和一个getter - (double)height;。您也应该从实现中删除getter,因为它已经使用属性声明生成了。

相关问题