3

These both work in my app without any noticeable difference:

1)

theArray = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:theData]];

2)

theArray = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:theData]];
[theArray retain];

However, are they really equivalent? (1) has an alloc statement, whereas (2) does not. Is one preferable over the other?

1 Answer 1

5

The effect is the same. But (2) is less efficient (a convenient method = alloc + init + autorelease).

  1. alloc → init
  2. alloc → init → autorelease → retain

The preferred way is not to copy the array.

theArray = [[NSKeyedUnarchiver unarchiveObjectWithData:theData] retain];

BTW, I notice that you have been asking a lot of basic questions about iPhone OS development. Please go through the tutorials on these first.

Sign up to request clarification or add additional context in comments.

2 Comments

Kenny, wouldn't it be cool if StackOverflow was the best resource on the web, even for "beginner" questions? In many ways, it is; however, redirecting those types of questions to your favorite tutorials isn't in-keeping with that vision. It's up to you.
Agreed. I'm a seasoned coder with decent apps under my belt and I still come here with questions like this. My least favorite answer to find here is "we're not here to teach to you program".. really? Cause then why are you here? Cause I'm here to learn and to help people learn. And often times I can get a quick answer to something much faster here than waiting through a 30 - 60 minute tutorial that starts with making a ios project.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.