5

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!

4

3 回答 3

19

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.

于 2012-11-14T15:45:20.660 回答
2

在第 2 行,您创建了一个新数组datesArray
然后,在第 6 行(for 循环的第一行),将新值设置为datesArray.

编译器只是警告您,第 2 行没有任何效果,并且代码存在错误(从某种意义上说,它没有达到您的预期)。
的确,programsArray可能是一个空数组,在这种情况下,您只想datesArray在您向我们展示的代码段之后初始化以使用它,但最好明确说明。

对于programsDict,它更容易:您将其初始化,... alloc] init]然后将其设置为 的对象datesArray,从而使第一个操作无用。

于 2012-11-14T15:51:40.423 回答
0

你没有在你的循环中使用 datesArray,你只是给它赋值,所以要么把它当作 nil 数组

NSMutableArray* datesArray = nil;

或喜欢

NSMutableArray *datesArray;

删除waring。

于 2016-06-20T19:11:23.217 回答