Question

FOR THE FOLLOWING CODE

 NSArray *colors = [[NSArray alloc]init];
if ([CONDITION]) {
    colors = @[
               [UIColor colorWithRed:240/255.f green:159/255.f blue:254/255.f alpha:1],
               [UIColor colorWithRed:255/255.f green:137/255.f blue:167/255.f alpha:1],
               [UIColor colorWithRed:126/255.f green:242/255.f blue:195/255.f alpha:1],
               [UIColor colorWithRed:119/255.f green:152/255.f blue:255/255.f alpha:1],
               [UIColor colorWithRed:240/255.f green:159/255.f blue:254/255.f alpha:1],
               ];
} else {
    colors = @[
               [UIColor colorWithRed:240/255.f green:159/255.f blue:254/255.f alpha:1],
               [UIColor colorWithRed:255/255.f green:137/255.f blue:167/255.f alpha:1],
               [UIColor colorWithRed:126/255.f green:242/255.f blue:195/255.f alpha:1],
               [UIColor colorWithRed:119/255.f green:152/255.f blue:255/255.f alpha:1],
               ];
}

This code is executed perfectly but during memory analysis ,I am getting error in static analyser "Dead Store" "Value stored to 'colors' during its initialization is never read", I am not sure how else can I execute this code.

Was it helpful?

Solution

Change the first line to

NSArray *colors;

It's enough to declare the variable. There's no need to create an empty array and set the variable to this unused instance.

Until the variable is set to the proper array (in the condition) its value will be nil under ARC and undefined in manual retain count. That's not relevant for your case as the value is never read before the assignment.

Edit: From the comment and other answer it still seems that people focus on the allocation of the unused array. Of course it is redundant—but the compiler warning is about the fact that a variable is assigned and then never read before the next assignment. If you would use a variable of type int the warning would be the same.

OTHER TIPS

As others have mentioned, there is no need to alloc/init the array where you declare the variable.

A couple of reasons:

The most obvious is that in the line

NSArray *colors = [[NSArray alloc]init]

you create an object and assign it to the variable, only to overwrite it immediately after in the assignments in both branches of the if-statement.

Whatever the condition of the if statement, your newly created array will be lost.

In practical terms, it will have a lifespan of (at most) a few microseconds, without ever being used for anything useful. This is what the static analyzer is trying to tell you when it says "Value stored to 'colors' during its initialization is never read" - reading the value of a variable is the first step to do anything useful with it. So if you never read it again, you might as well not assign it.

A slightly more subtle reason is that a plain NSArray is immutable, and calling alloc/init on an immutable collection class just creates an empty collection that can never be filled with anything useful.

There are cases where you do need an empty, immutable array, but they are not that many, and it is clearly not your case: immediately after the creation you go on to create arrays with some actual content and assign them to the variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top