Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 442
neowinston Avatar asked Nov 14 '12 15:11

neowinston


2 Answers

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.

like image 78
Paul.s Avatar answered Oct 26 '22 20:10

Paul.s


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.

like image 24
Guillaume Avatar answered Oct 26 '22 18:10

Guillaume