2011-07-18 5 views
1

それを信じるかどうか、私はこの質問をする前にインターネットを検索しました。信じられないほど、私はまだ作成する方法の良い明確な例を見つけることがありませんNSDictionariesのNSDictionaryNSDictionariesのNSDictionaryを作成する

ここまでは私のコードですが、nullを出力します。何か案は?

// Here I am creating the dictionaries in the code until I start getting them from the server ;) 
NSArray *keys = [NSArray arrayWithObjects:@"mission", @"target", @"distance",@"status", nil]; 


NSArray *objectsA = [NSArray arrayWithObjects:@"tiger", @"bill", @"5.4km", @"unknown", nil]; 
NSDictionary *tiger = [NSDictionary dictionaryWithObjects:objectsA 
                forKeys:keys]; 

NSArray *objectsB = [NSArray arrayWithObjects:@"bull", @"roger", @"10.1km", @"you are dead", nil]; 
NSDictionary *bull = [NSDictionary dictionaryWithObjects:objectsB 
               forKeys:keys]; 

NSArray *objectsC = [NSArray arrayWithObjects:@"peacock", @"geoff", @"1.4km", @"target liquidated", nil]; 
NSDictionary *peacock = [NSDictionary dictionaryWithObjects:objectsC 
                forKeys:keys]; 

// activeMissions = [NSArray arrayWithObjects:tiger, bull, peacock, nil]; 


[activeMissions setObject:tiger forKey:@"tiger"]; 
[activeMissions setObject:bull forKey:@"bull"]; 
[activeMissions setObject:peacock forKey:@"peacock"]; 

NSLog(@"active Missions %@", activeMissions); 

答えて

8

あなたはNSLog文がヌル(にObjCの戻りはnilではnilオブジェクトにメッセージを送信する)を印刷している理由です、activeMissionsをintializingされていません。

activeMissionsに割り当てる前にこれを入れて:

NSMutableDictionary *activeMissions = [NSMutableDictionary dictionaryWithCapacity:3]; 

をそうでない場合、あなたは非可変NSDictionaryを持つことを好む場合、あなたができる:

NSDictionary *activeMissions = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:tiger, bull, peacock, nil] 
                forKeys: [NSArray arrayWithObjects:@tiger, @"bull", @"peacock", nil]]; 

を(これは自動解放されていることを念頭に置いてあなたを保ちます何とかしておく必要があります)。

関連する問題