yashiganiの英傑になるまで死ねない日記

週末はマスターバイクでハイラルを走り回ります

initWithObjectsAndKeys: の落とし穴

なにかと便利な dictionary でとりわけ便利なのが initWithObjectsAndKeys: です
しかし,以下のようなときにはまるので要注意です

NSString *head = @"head";
NSString *body = nil;
NSString *tail = @"tail";

NSDictionary *dict =
     [NSDictionary dictionaryWithObjectsAndKeys:
         head, @"head",
         body, @"body",
         tail, @"tail", nil];

実はこのコードはエラーになりません
head キーのみを持つ dictionary ができます
setObject:forKey: なんかの object に nil を渡すと Xcode が教えてくれますが,こういうパターンでは教えてもらえないので注意が必要です