我有两个属性"M"和"m",虽然不是最好的编码风格,但请见谅。在init方法中对这些属性的赋值无法正常工作。以下是完整的代码:
#import "AppDelegate.h"
@interface Foo : NSObject
@property (nonatomic, assign) int M;
@property (nonatomic, assign) int m;
- (id)initWithM:(int)M m:(int)m;
@end
@implementation Foo
- (id)initWithM:(int)M m:(int)m {
if((self = [super init])) {
self.M = M;
printf("M = %d %d\n", M, self.M);
self.m = m;
printf("M = %d %d\n", M, self.M);
printf("m = %d %d\n", m, self.m);
}
return self;
}
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
Foo *f = [[Foo alloc] initWithM:2 m:1];
}
@end
下面是printf函数的输出:
M = 2 2
M = 2 1
m = 1 0
如果我将“M”改为“BAR”,将“m”改为“bar”,那么它的工作方式就符合我的期望。除了编译器错误之外,还有其他解释吗?
谢谢。
main.m:21:9: 合成属性'M'和'm'都声明了setter 'setM:' - 使用此setter将导致意外行为
。 - Sebastian