7

私には「M」と「m」の2つのプロパティがあります。これは、私が知っている最高のコーディングスタイルではありませんが、私には耐えられます。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」に変更すると、期待どおりに動作します。コンパイラのバグ以外にこれについての説明はありますか?

ありがとう。

4

1 に答える 1

12
@property int M;
@property int m;

両方が作成します

- (void)setM:(int)

mとプロパティの両方が本当にM必要な場合(絶対にすべきではありません)、使用できます

@property int M;
@property (setter = setLowerCaseM:, getter = lowerCaseM)int m;
于 2013-02-01T02:57:11.127 に答える