Why would my NSMutableDictionary be nil?

2020-05-09 17:23发布

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:

enter image description here

2条回答
smile是对你的礼貌
2楼-- · 2020-05-09 17:48

Initialize _dStockData

_dStockData = [[NSMutableDictionary alloc] init];
查看更多
倾城 Initia
3楼-- · 2020-05-09 17:53

It is nil because use initialize stockData

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

and print _dStockData

NSLog(@"Dictionary %@",_dStockData);
查看更多
登录 后发表回答