LoginSignup
18
21

More than 5 years have passed since last update.

プロトコルで宣言されたプロパティを使う

Last updated at Posted at 2014-05-10

ちょっと考えれば当たり前なのですが地味にハマったのでメモ

プロトコル宣言中に@propertyでプロパティを宣言することが出来ます。

@protocol MyPropertyProtocol <NSObject>

@property (nonatomic) NSString *myProperty;

@end

で、このプロトコルを利用して宣言したプロパティを利用しようとするとunrecognized selectorでクラッシュします。

self.myProperty = @"プロパティ";

なのでどうするかというと@synthesizeを使います。

@synthesize myProperty = _myProperty;

※オートシンセサイズに合わせてアンダースコア付きの変数にしています。(ikesyoさんありがとうございます。)

自分でアクセサメソッド実装したりしてめんどくさいことしてましたがこの1行で解決しました。
Modern ObjCで@synthesize書かなくても良くなったので存在を忘れかけてましたがまだ使いドコロがあったんですね。

18
21
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
21