2

I have been tasked with "cleaning up" someone else's Objective-C code. I will admit, it is certainly not my favorite language.

One method i found in this user's code that seems redundant to me is this:

if (favoriteItemsArray || [favoriteItemsArray count] > 0) {
    [favoriteItemsArray removeAllObjects];
    favoriteItemsArray = nil;
}

if (favoriteOrderArray || [favoriteOrderArray count] > 0) {
    [favoriteOrderArray removeAllObjects];
    favoriteOrderArray = nil;
}

favoriteItemsArray = [[NSMutableArray alloc] init];
favoriteOrderArray = [[NSMutableArray alloc] init];

I wanted to double check with you all and see if i am just too use to JAVA, but couldn't this code be condensed to just the last two lines and just merely say:

favoriteItemsArray = [[NSMutableArray alloc] init];
favoriteOrderArray = [[NSMutableArray alloc] init];

If not can someone explain?

Again this is not my code..

11
  • I am trying to think of some vague situation of retention that requires removing the objects before reinitializing, but I can't come up with one. I'd say go ahead with your approach. Commented Jul 28, 2014 at 19:27
  • 1
    Those two if statements are bad. The || part is pointless. Commented Jul 28, 2014 at 19:28
  • Are the last two lines inside or outside the 1st if statement? You don't show the closing curly brace. Commented Jul 28, 2014 at 19:29
  • the || is pointless as well, I agree.. the two init/alloc statements are out side both if conditions Commented Jul 28, 2014 at 19:30
  • @rmaddy All braces are there Commented Jul 28, 2014 at 19:30

1 Answer 1

5

You are correct. You can remove the first part of the code (if ARC is used for reference counting). When the arrays are reassigned, the previous array will be freed because the retain count will reach zero and all objects in the array will also be released.

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

1 Comment

i will accept this answer in a few minutes (StackOverFlow won't let me yet.. that was an easy reputation booster!

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.