Help us understand the problem. What is going on with this article?

ブロック内でプリミティブ型のインスタンス変数にアクセスする

More than 5 years have passed since last update.

ARC環境のObjective-Cでブロック内でインスタンス変数にアクセスする場合のメモ。
ポインタ変数ならば__block修飾子を使ってretain cycleを回避すればいいのだけど、変数が整数や真偽値などのプリミティブ型だった場合機能しないので以下のように参照を使う。

main.m
@interface SomeClass () {
  BOOL _flag;
}
main.m
[self performBlock:^(id sender) {
  // retain cycle が起きる
  _flag = YES;
} afterDelay:(NSTimeInterval)]
main.m
__block BOOL __flag = _flag
[self performBlock:^(id sender) {
  // ポインタではないので機能しない
  __flag = YES;
} afterDelay:(NSTimeInterval)]

※Ushioさんのご指摘を受けたため修正しました。
ブロック内にインスタンス変数へのポインタをキャプチャしてしまうとselfが破棄されたかどうかの判断がつかないそうです。以下は、KVアクセッサを使ってインスタンス変数にブロック内でアクセスする方法です。

main.m
__block __weak typeof(self) __self = self;
[self performBlock:^(id sender) {
  // KV使ってインスタンス変数にアクセスする
  [__self setValue:YES forKey:@"_flag"];
  BOOL flag = [__self valueForKey:@"flag"] boolValue];
} afterDelay:(NSTimeInterval)]

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした