将数据添加到NSDictionary ... SRSLY?有没有更快的方法?

时间:2010-08-12 12:11:57

标签: iphone objective-c nsmutabledictionary nsnumber verbose

我来自Flash ActionScript背景。我来自哪里,我可以像这样设置类似字典的对象:

var data:Object = {startPoint:5, endPoint:12};

所以来到Objective-C,我惊讶地发现相当于这个似乎是:

NSMutableDictionary *data = [NSMutableDictionary dictionary];
[data setObject:[NSNumber numberWithInt:5] forKey:@"startPoint"];
[data setObject:[NSNumber numberWithInt:12] forKey:@"endPoint"];

当然有一种更简单的方法......或者这真的很重要吗?

1 个答案:

答案 0 :(得分:3)

这是我能想到的唯一更短的方式:

NSMutableDictionary *petalData = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"startPoint", [NSNumber numberWithInt:12], @"endPoint", nil];