57

I know I can create an NSArray with @[@"foo", @"bar"] or an NSDictionary with @{@0 : @"foo", @1 : @"bar"}.

Is there a literal syntax for creating an NSMutableArray or an NSMutableDictionary?

1
  • Just don't forget that its NSDictionary *dictionary = @{@"key" : @"value"};, might be confusing with he way you have it written. Different from :objectsWithKeys.
    – serge-k
    Mar 15, 2016 at 16:13

5 Answers 5

104

There isn't a built in way, but I just usually use mutableCopy like this:

NSMutableArray *array = [@[ @"1", @"2", @"3" ] mutableCopy];
9
  • 5
    This seems less efficient and not much shorter than [NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil].
    – ma11hew28
    Sep 19, 2012 at 1:19
  • 1
    Or alternatively: "NSMutableArray *array = [NSMutableArray arrayWithArray:@[ @"1", @"2", @"3" ]];" - though I like your example better :)
    – Marchy
    Nov 28, 2012 at 18:45
  • There is a builtin way in NSJSONSerialization. Apr 29, 2013 at 20:44
  • 1
    @MarkAmery How is that less hacky? The literal syntax expands to +[NSArray arrayWithObjects:count:], not arrayWithObjects so the literal syntax validates that all items are non-nil. Jul 29, 2013 at 17:31
  • 1
    You can also do NSMutableArray *array = @[].mutableCopy; which seems more readable.
    – Matt Mc
    Jan 6, 2015 at 3:28
18

No. Just as how there isn't a syntax for creating an NSMutableString either. Mutable objects are not particularly suited to literal values.

8
  • 20
    Have to disagree with that last sentence. Programming in Python, for example, collections are created literally and mutable by default. It can be very handy.
    – jscs
    Sep 14, 2012 at 6:27
  • @JoshCaswell: Does python even have immutable collections? Sep 14, 2012 at 7:16
  • 6
    Yes, there's things called "tuples" which are immutable. > (1, 2, 3) # tuple (immutable array) > [1, 2, 3] # list (mutable array)
    – jscs
    Sep 14, 2012 at 7:49
  • 2
    as posted below you do the following NSMutableArray *list = [@[] mutableCopy]; i.e. you add mutableCopy at the end. That is how the literal is specified
    – maninvan
    Jan 10, 2016 at 20:32
  • 1
    @maninvan That's not a literal. That's an expression that uses a literal. Jan 12, 2016 at 23:07
17

But, is there a literal syntax for creating an NSMutableArray or an NSMutableDictionary?

No. Best alternative:

[@[ @"foo", @"bar"] mutableCopy]
9

Yes. But not quite. Take a look at this;

NSMutableArray *list = [@[] mutableCopy];

This creates a non-mutable array @[] and calls mutableCopy which returns a NSMutableArray *. In place of @[], you can give any array literal.

3
  • Do you know of any issues arising from using this in lieu of the longhand? Mar 22, 2017 at 9:43
  • @JohnnyRockex there are no issues with this syntax, but it's pretty ugly if the array is empty to start with ;) Nov 10, 2021 at 23:05
  • I love me some shorthand was just wondering if had a (technical) effect Nov 11, 2021 at 12:41
3

If you have a nested literal of arrays and dictionaries, you can turn this into a fully mutable version by going through NSJSONSerialization. For example:

NSArray* array = @[ @{ @"call" : @{ @"devices" : @[ @"$(devices)" ] } } ];
NSData* data   = [NSJSONSerialization dataWithJSONObject:array 
                                                 options:0 
                                                   error:nil];

NSJSONReadingOptions options = NSJSONReadingMutableContainers | 
                               NSJSONReadingMutableLeaves;
NSMutableArray* mutableArray = [NSJSONSerialization JSONObjectWithData:data 
                                                               options:options
                                                                 error:nil];

It's a bit of a detour, but at least you don't have to write out the code yourself. And the good thing is that NSJSONSerialization is very fast.

4
  • 1
    Uh dude, JSONKit is faster. By 5x. Plus, this is too circuitous. Just use -mutableCopy, then you can optionally use -autorelease.
    – Nate Symer
    Jul 12, 2013 at 0:00
  • 1
    @NathanielSymer Come on dude: The two year old JSONKit readme --well maintained stuff btw-- itself says it was just 25% - 40% faster. And -mutableCopy only does a shallow copy. The only way is to do something 'circuitous'. Jul 12, 2013 at 4:48
  • 1
    you could also just do this: [[NSMutableArray alloc] initWithArray:@[@"A",@"B"]]. sorry for the downvote, but serializing and deserializing is insane and won't work with many types of objects. Apr 22, 2014 at 13:23
  • 2
    @kritzikratzi First, insane? You're taking a very simple example, not my example. Please write out @[ @{ @"call" : @{ @"devices" : @[ @"$(devices)" ] } } ] and you'll see how insane that is. Second, this works for the types given in @MattDiPasquale's and my example. Even more, I explicitly mention that this works for literals. So please don't apologise! Apr 26, 2014 at 5:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.