What sense to copy empty array?

179 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 best solutions below

2
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.