4
我偶然发现了Objective-C中的一些奇怪行为。 我有一个的main.m:为属性创建我自己的setter时的无限递归
#include <Foundation/Foundation.h>
#include "AClass.h"
int main(int argc, char* argv[]) {
AClass* tmpClass = [[AClass alloc] init];
[tmpClass setAVariable:12];
return -1;
}
报头AClass.h:
#include <Foundation/Foundation.h>
@interface AClass: NSObject;
-(void) setAVariable:(int) bVariable;
@property int aVariable;
@end
和一个对应的实现文件AClass.m:
#include <Foundation/Foundation.h>
#include <AClass.h>
@implementation AClass
@dynamic aVariable;
int aVariable;
-(void) setAVariable:(int)bVariable {
NSLog(@"foo:");
self.aVariable = bVariable;
}
@end
当编译该代码与任一在Linux上的铿锵声或通过OSX上的Xcode触发无限递归。 我不知道这是否是clang/Objective-C中的错误。
你应该知道,你所声称的'aVariable'是一个全球性的,而不是你所期待的伊娃。它需要位于'@ implementation'块顶部的大括号中:'@implementation AClass {int aVariable; }/*等*/@结束' –
这是我的意图:) –