Unused variable warning for each loop

I have an array of NSNumber objects created this way:

        myArray = [[NSMutableArray alloc] initWithObjects:[NSNumber numberWithDouble:0.0],
                [NSNumber numberWithDouble:0.0],
                [NSNumber numberWithDouble:0.0],
                [NSNumber numberWithDouble:0.0],
                [NSNumber numberWithDouble:0.0], nil];

(Although it just occurred to me that I could do

myArray = [NSMutableArray arrayWithObjects: object1, etc..., nil];

and completely missed the selection. What would be better?)

Anyway, this concerns my question:

Over the life of the application, the values ​​change. At some point, I want all reset to be zero. Here's how I do it now:

 for (NSNumber *number in myArray) {
     number = [NSNumber numberWithDouble:0.0];
 }

But the static analyzer generates a warning, because it considers that the "number" is an unused variable (which it is technically set, and then never used again). Is there a better way to reset all array elements? Perhaps replace the array with a new one? What would be the fastest and avoid the static analysis warning?

+3
1

, , alloc + initWithObjects: , arrayWithObjects: ( , ).

for (NSNumber *number in myArray) {
    number = [NSNumber numberWithDouble:0.0];
}

, . number NSNumber, , , number, NSNumber, NSNumber myArray.

NSNumber , - myArray [NSNumber numberWithDouble:0.0]

NSUInteger count = [myArray count];
[myArray removeAllObjects];

while(count--)
    [myArray addObject:[NSNumber numberWithDouble:0.0]];
+7

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


All Articles