简体   繁体   中英

Objective-C and ARC: Why value stored to during its initialization is never read?

I'm using this code with ARC :

NSMutableDictionary *datesDict = [[NSMutableDictionary alloc]init];
NSMutableArray *datesArray = [[NSMutableArray alloc]init];

for (NSString *key in programsArray) {

    datesArray = [_onDemandDictionary objectForKey:key];
    NSMutableArray *newDates = [[NSMutableArray alloc]init];
    int count;
    for (count = 0; count <datesArray.count; count++) {
        NSMutableDictionary *programsDict = [[NSMutableDictionary alloc]init];
        programsDict = [datesArray objectAtIndex:count];
        [newDates addObject:[programsDict objectForKey:@"date"]];

    }

    [datesDict setObject:newDates forKey:key];
}

But when I run the analyzer tool I'm getting value stored to (datesArray and programsDict) during its initialization is never read on lines:

NSMutableArray *datesArray = [[NSMutableArray alloc]init];
programsDict = [datesArray objectAtIndex:count];

Why is this happening how do I get hid of the warning?

Thank you!

The issue is you create a new NSMutableArray and assign it to datesArray at the beginning

NSMutableArray *datesArray = [[NSMutableArray alloc]init];

Then almost immediately after you assign a completely different value to datesArray with

datesArray = [_onDemandDictionary objectForKey:key];

I would just start with

NSMutableArray *datesArray = nil;

It's the same concept for programsDict .

On line 2, you create a new array datesArray .
Then, on line 6 (first line of the for loop), you set a new value to datesArray .

The compiler is just warning you that the line 2 has no effect, and that the code is bugged (in the sense it does not do what you expect).
True, the programsArray could be an empty array, and in this case you want datesArray to just be initialized to use it after the snippet you showed us, but it would be better to make this explicit.

For programsDict , it is even easier: you initialize it with ... alloc] init] then set it to an object of datesArray , making the first operation useless.

You are not using datesArray in your loop, you are simply assigning it values, So either take it nil array like

NSMutableArray* datesArray = nil;

or like

NSMutableArray *datesArray;

to remove waring .

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