Reputation: 4958
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..
Upvotes: 2
Views: 398
Reputation: 171854
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.
Upvotes: 5