为什么我的NSMutableDictionary会为零?

时间:2014-04-16 10:25:03

标签: ios objective-c nsarray nsmutabledictionary

我正在尝试将数组存储在NSMutableDictionary中。但是,在我将对象设置为它之后,NSMutableDictionary为null。这是我的代码,任何帮助表示赞赏:

NSMutableArray *arrTemp = [[NSMutableArray alloc] init];
NSMutableDictionary *dTemp = [[NSMutableDictionary alloc] init];
STStockData *stockData = [[STStockData alloc] init];
for (int i = 0; i < [_arrTickers count]; i++) {
    // get the ticker from its json form
    dTemp = [_arrTickers objectAtIndex:i];
    NSLog(@"Ticker: %@",[dTemp objectForKey:@"ticker"]);
    // gets current data for ticker
    [arrTemp addObjectsFromArray:[stockData getCurrentStockDataForTicker:[dTemp objectForKey:@"ticker"]]];
    NSLog(@"Price %@",[arrTemp objectAtIndex:1]); // just to prove the array isnt nil.
    // adds it to the dictionary
    [_dStockData setObject:arrTemp forKey:[dTemp objectForKey:@"ticker"]];
    NSLog(@"Dictionary %@",_dStockData);
    // remove all objects so can reuse.
    [arrTemp removeAllObjects];
    dTemp = nil; // can't remove objects using [removeAllObjects] method. believe its due to it holding inside NSArrays which are immutable.

}

这是控制台输出:

enter image description here

2 个答案:

答案 0 :(得分:1)

初始化_dStockData

_dStockData = [[NSMutableDictionary alloc] init];

答案 1 :(得分:0)

它是零,因为使用初始化stockData

STStockData *stockData = [[STStockData alloc] init];

并打印_dStockData

NSLog(@"Dictionary %@",_dStockData);