简体   繁体   中英

Objective-C NSMutableArray alloc init on already declared Object

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM