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

I use 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 get the value stored (dateArray and programDict) during its initialization is never read in lines:

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

Why is this happening, how can I hide the warning?

Thanks!

+4
source share
3 answers

The problem is that you create a new NSMutableArray and assign its datesArray at the beginning

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

Then almost immediately after you assign datesArray completely different value

 datesArray = [_onDemandDictionary objectForKey:key]; 

I would start with

 NSMutableArray *datesArray = nil; 

This is the same concept for programsDict .

+17
source

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

The compiler simply warns you that line 2 is not working, and that the code is listening (in the sense that it does not do what you expect).
True, programsArray can be an empty array, in which case you want datesArray just initialize to use it after the fragment that you showed us, but it would be better to make it explicit.

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

+2
source

You do not use dateArray in your loop, you just assign it to values. So either take it in a nil array like

 NSMutableArray* datesArray = nil; 

or how

 NSMutableArray *datesArray; 

to remove waring.

0
source

Source: https://habr.com/ru/post/1446028/


All Articles