objc-zen/objc-zen-book

`mutableCopy` addtion

Closed this issue · 2 comments

Although NSMutableArray *aMutableArray = [@[] mutableCopy]; is not recommended, the code below is appropriate:

NSMutableDictionary *aMutableDictionary = [@{@"Key":@"Value",
                                             @"AnotherKey":@"OtherValue",
                                             @"SomeOtherKey":@"SomeOtherValue"} mutableCopy];

if (SomeThingWrong) {
    aMutableDictionary[@"Error"] = @"ErrorDescription";
}

I don't think this is recommended either. You're still allocating an object (NSDictionary via the literal) that is going to be discarded immediately but not released until the autorelease pool drains.
@albertodebortoli do you want to weight in?

I have to agree with @lukabernardi. The dictionary is discarded immediately; I consider this and similar shortcuts as ways to reduce the verbosity imposed by the language.