Nimit Pattanasri
Nimit Pattanasri

Reputation: 1602

Does ARC eliminate the need for temporary variables?

Without ARC-enabled, we write

NSArray *items = [[NSArray alloc] initWithObjects:
                  @"a",
                  @"b",
                  @"c",
                  @"d",
                  nil];

self.allItems = items;
[items release];

I just wonder whether we can take a shortcut with ARC-enabled like this:

self.allItems = [[NSArray alloc] initWithObjects:
                  @"a",
                  @"b",
                  @"c",
                  @"d",
                  nil];

Can we eliminate items when we use ARC? What's the best practice?

Upvotes: 4

Views: 109

Answers (3)

Andrew Madsen
Andrew Madsen

Reputation: 21383

Yes, the second example you showed is just fine under ARC, and is probably desirable because it's more concise.

Upvotes: 3

Kevin Sylvestre
Kevin Sylvestre

Reputation: 38052

The second example will work and is the best practice now. With ARC you no longer need to call retain or release.

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 385870

Yes, you can eliminate the items variable if you're using ARC. I suggest you eliminate it if you don't need it elsewhere and you don't think it makes your code easier to understand. I would definitely eliminate it in your example.

Upvotes: 1

Related Questions