Why would my NSMutableDictionary be nil?

独自空忆成欢 提交于 2019-12-20 07:57:07

问题


I am trying to store an array in a NSMutableDictionary. However the NSMutableDictionary is null after i have set objects to it. Here is my code any help is appreciated:

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.

}

Here is the console output:


回答1:


Initialize _dStockData

_dStockData = [[NSMutableDictionary alloc] init];



回答2:


It is nil because use initialize stockData

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

and print _dStockData

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


来源:https://stackoverflow.com/questions/23106730/why-would-my-nsmutabledictionary-be-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!