Objective-C at 符号和花括号,@{ ... } 是什么意思? [英] Objective-C at sign and curly braces, @{ ... } what does it mean?

查看:102
本文介绍了Objective-C at 符号和花括号,@{ ... } 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Objective-C 中有这一行.

I have this line in Objective-C.

NSMutableArray *mutableArray;
[mutableArray addObject:@{ @"Something" : aObject, @"Otherthing" : anotherObject }];

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

What does the @{ ... } part do exactly? It is an object, but it seems to create some kind of key, value pair on the fly.

推荐答案

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

It is creating NSDictionary object as you said. Syntax is simple

NSDictionary* dictionary = @{key: object, key: object};

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

In your example, keys are objects of NSString class. It is important to remember that dictionary copies keys and retains values.

这篇关于Objective-C at 符号和花括号,@{ ... } 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆