Objective-C at 符号和花括号,@{ ... } 是什么意思?

uno*_*nom 3 objective-c nsdictionary objective-c-literals

我在 Objective-C 中有这一行。

NSMutableArray *mutableArray;
[mutableArray addObject:@{ @"Something" : aObject, @"Otherthing" : anotherObject }];
Run Code Online (Sandbox Code Playgroud)

@{ ... }部分具体做什么?它是一个对象,但它似乎动态地创建了某种键值对。

Mic*_*iak 6

正如您所说,它正在创建 NSDictionary 对象。语法很简单

NSDictionary* dictionary = @{key: object, key: object};
Run Code Online (Sandbox Code Playgroud)

在您的示例中,键是 NSString 类的对象。重要的是要记住字典复制键并保留值。