Winston Winston - 5 months ago 43
Objective-C Question

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!

Answer

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.

Comments