17
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NSMutableArrayスコープを最小限にする

Posted at

正直、初見の時の可読性が落ちるので微妙かもしれませんが、
発想の1つとしてメモしてみます。
blocksをその場ですぐ使う事で、NSMutableArrayのスコープを最小限にして、その後をNSArrayとして固定してしまいます。
NSMutableArrayがだらだらと下に残ってしまうのを防ぐ事が出来ます。
Mutable系のオブジェクトでは全て可能です。

NSArray *array = ^{
    NSMutableArray *r = [NSMutableArray array];
    for(int i = 0 ; i < 100 ; ++i)
    {
        [r addObject:@(i)];
    }
    return r;
}();
for(NSNumber *n in array)
{
    NSLog(@"%@", n);
}
17
17
3

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
17
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?