6

Possible Duplicate:
What are the details of “Objective-C Literals” mentioned in the Xcode 4.4 release notes?

I've got question about @[] and @{}.

Some code taken from internet:

self.searches = [@[] mutableCopy]; 
self.searchResults = [@{} mutableCopy]; 
  1. Is @[] equal to [NSMutableDictionary dictionary]?
  2. Is @{} equal to [NSMutableArray array]?
Community
  • 1
  • 1
Tomasz Szulc
  • 4,217
  • 4
  • 43
  • 79

3 Answers3

10
  1. No. @[] is equal to [NSArray array] or [[NSArray alloc] init].
  2. No. @{} is equal to [NSDictionary dictionary] or [[NSDictionary alloc] init].

(depending on the context and whether you use Automatic Reference Counting (ARC) or not)

That's why you see things like [@[] mutableCopy] sometimes. This will create an empty immutable array and create a mutable copy of it.

The result is the same as using [NSMutableDictionary dictionary] or [[NSMutableDictionary alloc] init].

DrummerB
  • 39,814
  • 12
  • 105
  • 142
1

@{} is equal to [NSDictionary dictionary]

@[] is equal to [NSArray array]

so [@[] mutableCopy] creates an empty immutable object and then it makes a mutablecopy of it. I don't think you can do it less efficient.

Bastian
  • 10,403
  • 1
  • 31
  • 40
0

First one @[] is a shorthand to create an Array, for example the following array of two elements:

NSArray *array = @[ @"First", @"Second"];

Second one @{} creates a dictionary, for example:

NSDictionary *dictionary = @{
    @"first" : someValue,
    @"second" : someValue,
};
alemangui
  • 3,571
  • 22
  • 33