如何添加到NSDictionary

时间:2021-07-30 13:13:36

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

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

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?

我想在字典中将键保存为NSString,值保存为int。这是怎么做的?第二,可变字典和普通字典有什么区别?

5 个解决方案

#1


180  

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

可变字典可以更改,例如,您可以添加和删除对象。一个不可变的东西一旦被创建就会被修复。

create and add:

创建和添加:

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

and retrieve:

和检索:

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

#2


30  

By setting you'd use setValue:(id)value forKey:(id)key method of NSMutableDictionary object:

通过设置可以使用setValue:(id)value forKey:(id) NSMutableDictionary对象的key方法:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:[NSNumber numberWithInt:5] forKey:@"age"];

Or in modern Objective-C:

或在现代objective - c:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict[@"age"] = @5;

The difference between mutable and "normal" is, well, mutability. I.e. you can alter the contents of NSMutableDictionary (and NSMutableArray) while you can't do that with "normal" NSDictionary and NSArray

可变和“正常”之间的区别是,嗯,可变。例如,你可以改变NSMutableDictionary(和NSMutableArray)的内容,而你不能用“普通”的NSDictionary和NSArray

#3


12  

When ever the array is declared, then only we have to add the key-value's in NSDictionary like

当数组被声明时,我们只需要在NSDictionary中添加键值

NSDictionary *normalDict = [[NSDictionary alloc]initWithObjectsAndKeys:@"Value1",@"Key1",@"Value2",@"Key2",@"Value3",@"Key3",nil];

we cannot add or remove the key values in this NSDictionary

我们不能在这个NSDictionary中添加或删除键值。

Where as in NSMutableDictionary we can add the objects after intialization of array also by using this method

就像在NSMutableDictionary中一样,我们也可以使用这个方法在数组的初始化之后添加对象

NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc]init];'
[mutableDict setObject:@"Value1" forKey:@"Key1"];
[mutableDict setObject:@"Value2" forKey:@"Key2"];
[mutableDict setObject:@"Value3" forKey:@"Key3"];

for removing the key value we have to use the following code

为了删除键值,我们必须使用以下代码

[mutableDict removeObject:@"Value1" forKey:@"Key1"];

#4


8  

Update version

Objective-C

objective - c

Create:

创建:

NSDictionary *dictionary = @{@"myKey1": @7, @"myKey2": @5}; 

Change:

变化:

NSMutableDictionary *mutableDictionary = [dictionary mutableCopy];     //Make the dictionary mutable to change/add
mutableDictionary[@"myKey3"] = @3;

The short-hand syntax is called Objective-C Literals.

简短的语法称为Objective-C文字。

Swift

斯威夫特

Create:

创建:

var dictionary = ["myKey1": 7, "myKey2": 5]

Change:

变化:

dictionary["myKey3"] = 3

#5


1  

You want to ask is "what is the difference between a mutable and a non-mutable array or dictionary." Many times there different terms are used to describe things that you already know about. In this case, you can replace the term "mutable" with "dynamic." So, a mutuable dictionary or array is one that is "dynamic" and can change at runtime, whereas a non-mutable dictionary or array is one that is "static" and defined in your code and does not change at runtime (in other words, you will not be adding, deleting or possibly sorting the elements.)

你想问的是“可变数组和非可变数组或字典之间的区别是什么?”很多时候,有不同的术语用来描述你已经知道的事情。在这种情况下,您可以将术语“mutablecopy”替换为“dynamic”。mutuable字典或数组是一个“动态”和可以在运行时改变,而non-mutable字典或数组中定义是“静态”和你的代码,在运行时不会改变(换句话说,你将不会添加、删除或排序的元素)。

As to how it is done, you are asking us to repeat the documentation here. All you need to do is to search in sample code and the Xcode documentation to see exactly how it is done. But the mutable thing threw me too when I was first learning, so I'll give you that one!

关于如何做到这一点,您要求我们在这里重复一下文档。您所需要做的就是在示例代码和Xcode文档中搜索,以确切地了解如何实现它。但是当我第一次学习的时候,那个易变的东西也把我给扔了,所以我给你那个!

#1


180  

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

可变字典可以更改,例如,您可以添加和删除对象。一个不可变的东西一旦被创建就会被修复。

create and add:

创建和添加:

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

and retrieve:

和检索:

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

#2


30  

By setting you'd use setValue:(id)value forKey:(id)key method of NSMutableDictionary object:

通过设置可以使用setValue:(id)value forKey:(id) NSMutableDictionary对象的key方法:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:[NSNumber numberWithInt:5] forKey:@"age"];

Or in modern Objective-C:

或在现代objective - c:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict[@"age"] = @5;

The difference between mutable and "normal" is, well, mutability. I.e. you can alter the contents of NSMutableDictionary (and NSMutableArray) while you can't do that with "normal" NSDictionary and NSArray

可变和“正常”之间的区别是,嗯,可变。例如,你可以改变NSMutableDictionary(和NSMutableArray)的内容,而你不能用“普通”的NSDictionary和NSArray

#3


12  

When ever the array is declared, then only we have to add the key-value's in NSDictionary like

当数组被声明时,我们只需要在NSDictionary中添加键值

NSDictionary *normalDict = [[NSDictionary alloc]initWithObjectsAndKeys:@"Value1",@"Key1",@"Value2",@"Key2",@"Value3",@"Key3",nil];

we cannot add or remove the key values in this NSDictionary

我们不能在这个NSDictionary中添加或删除键值。

Where as in NSMutableDictionary we can add the objects after intialization of array also by using this method

就像在NSMutableDictionary中一样,我们也可以使用这个方法在数组的初始化之后添加对象

NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc]init];'
[mutableDict setObject:@"Value1" forKey:@"Key1"];
[mutableDict setObject:@"Value2" forKey:@"Key2"];
[mutableDict setObject:@"Value3" forKey:@"Key3"];

for removing the key value we have to use the following code

为了删除键值,我们必须使用以下代码

[mutableDict removeObject:@"Value1" forKey:@"Key1"];

#4


8  

Update version

Objective-C

objective - c

Create:

创建:

NSDictionary *dictionary = @{@"myKey1": @7, @"myKey2": @5}; 

Change:

变化:

NSMutableDictionary *mutableDictionary = [dictionary mutableCopy];     //Make the dictionary mutable to change/add
mutableDictionary[@"myKey3"] = @3;

The short-hand syntax is called Objective-C Literals.

简短的语法称为Objective-C文字。

Swift

斯威夫特

Create:

创建:

var dictionary = ["myKey1": 7, "myKey2": 5]

Change:

变化:

dictionary["myKey3"] = 3

#5


1  

You want to ask is "what is the difference between a mutable and a non-mutable array or dictionary." Many times there different terms are used to describe things that you already know about. In this case, you can replace the term "mutable" with "dynamic." So, a mutuable dictionary or array is one that is "dynamic" and can change at runtime, whereas a non-mutable dictionary or array is one that is "static" and defined in your code and does not change at runtime (in other words, you will not be adding, deleting or possibly sorting the elements.)

你想问的是“可变数组和非可变数组或字典之间的区别是什么?”很多时候,有不同的术语用来描述你已经知道的事情。在这种情况下,您可以将术语“mutablecopy”替换为“dynamic”。mutuable字典或数组是一个“动态”和可以在运行时改变,而non-mutable字典或数组中定义是“静态”和你的代码,在运行时不会改变(换句话说,你将不会添加、删除或排序的元素)。

As to how it is done, you are asking us to repeat the documentation here. All you need to do is to search in sample code and the Xcode documentation to see exactly how it is done. But the mutable thing threw me too when I was first learning, so I'll give you that one!

关于如何做到这一点,您要求我们在这里重复一下文档。您所需要做的就是在示例代码和Xcode文档中搜索,以确切地了解如何实现它。但是当我第一次学习的时候,那个易变的东西也把我给扔了,所以我给你那个!