如何添加到NSDictionary [英] How to add to an NSDictionary

查看:152
本文介绍了如何添加到NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NSMutableArray 并意识到使用字典对于我想要实现的目标来说要简单得多。

I was using a NSMutableArray and realized that using a dictionary is a lot simpler for what I am trying to achieve.

我想在字典中将密钥保存为 NSString ,并将值保存为 int 。这是怎么做到的?其次,mutable和普通字典有什么区别?

I want to save a key as a NSString and a value as an int in the dictionary. How is this done? Secondly, what is the difference between mutable and a normal dictionary?

推荐答案

mutable 字典可以是已更改,即您可以添加和删除对象。
immutable 一旦创建就修复了。

A mutable dictionary can be changed, i.e. you can add and remove objects. An immutable is fixed once it is created.

创建并添加:

NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
[dict setObject:[NSNumber numberWithInt:42] forKey:@"A cool number"];

并检索:

int myNumber = [[dict objectForKey:@"A cool number"] intValue];

这篇关于如何添加到NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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