What sense to copy empty array?

156 views Asked by At

I were reading another programmer's code, so i found this:

@property(nonatomic, strong) NSArray *assets;
----
   _assets = [@[] mutableCopy];
    __block NSMutableArray *tmpAssets = [@[] mutableCopy];

Is it some kind of trick? Why does he used mutableCopy to immutable array assets ? Why doesn't he just create it like:

self.assets = [NSArray new];
__block NSMutableArray *tmpAssets = [NSMutableArray new];

?

1

There are 1 answers

2
cacau On

Just a shorthand to get an empty mutable array.

Instead of [NSMutableArray alloc] init] it's slightly less code to write -

the new construct isn't really used by the majority of Objective-C programmers and was added as a convenience to newcomers from other languages.

Mind that @[] will create an (immutable) NSArray -
there is no Objective-C literal for NSMutableArray.