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
?
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
?
No. Just as how there isn't a syntax for creating an NSMutableString
either. Mutable objects are not particularly suited to literal values.
> (1, 2, 3) # tuple (immutable array)
> [1, 2, 3] # list (mutable array)
–
Neogothic NSMutableArray *list = [@[] mutableCopy];
i.e. you add mutableCopy at the end. That is how the literal is specified –
Tocology mutableCopy
on the result. The expression [@[] mutableCopy]
simply cannot be called a literal. Even if it was optimized, it's still not a literal. –
Phytography There isn't a built in way, but I just usually use mutableCopy
like this:
NSMutableArray *array = [@[ @"1", @"2", @"3" ] mutableCopy];
[NSMutableArray arrayWithObjects:@"1", @"2", @"3", nil]
. –
Tedda NSJSONSerialization
. –
Manganate NSMutableArray arrayWithObjects:...
, is presumably less efficient for what little it matters, and - to my eye - slightly less readable too. A concise literal syntax would've been nice, but if this is the nearest thing to it, then I think I'll stick to the less hacky-feeling arrayWithObjects
for initialising mutable arrays. –
Rouge +[NSArray arrayWithObjects:count:]
, not arrayWithObjects
so the literal syntax validates that all items are non-nil. –
Tramroad NSMutableArray
with one of its own initialiser methods is what feels hacky to me. –
Rouge NSMutableArray *array = @[].mutableCopy;
which seems more readable. –
Sofko No. Just as how there isn't a syntax for creating an NSMutableString
either. Mutable objects are not particularly suited to literal values.
> (1, 2, 3) # tuple (immutable array)
> [1, 2, 3] # list (mutable array)
–
Neogothic NSMutableArray *list = [@[] mutableCopy];
i.e. you add mutableCopy at the end. That is how the literal is specified –
Tocology mutableCopy
on the result. The expression [@[] mutableCopy]
simply cannot be called a literal. Even if it was optimized, it's still not a literal. –
Phytography But, is there a literal syntax for creating an NSMutableArray or an NSMutableDictionary?
No. Best alternative:
[@[ @"foo", @"bar"] mutableCopy]
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.
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.
-mutableCopy
only does a shallow copy. The only way is to do something 'circuitous'. –
Manganate @[ @{ @"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! –
Manganate © 2022 - 2024 — McMap. All rights reserved.
NSDictionary *dictionary = @{@"key" : @"value"};
, might be confusing with he way you have it written. Different from:objectsWithKeys
. – Riles