Objective c 在现有NSMutableArray中添加新值和键

Objective c 在现有NSMutableArray中添加新值和键,objective-c,nsmutablearray,nsdictionary,Objective C,Nsmutablearray,Nsdictionary,但是,我有以下数组,是否有方法在以下初始化之后为NSMutableArray中的每个字典项添加新的键和值(例如年龄) NSMutableArray* names = [NSMutableArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys: @"Joe",@"firstname", @"Bloggs",@"

但是,我有以下数组,是否有方法在以下初始化之后为
NSMutableArray
中的每个字典项添加新的
键和
值(例如年龄)

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Joe",@"firstname",
                   @"Bloggs",@"surname",
                   nil],
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Simon",@"firstname",
                   @"Templar",@"surname",
                   nil],
                  [NSDictionary dictionaryWithObjectsAndKeys:
                   @"Amelia",@"firstname",
                   @"Pond",@"surname",
                   nil],
                  nil];

如果可以将字典数组定义为可变字典,则可以执行以下操作:

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Joe",@"firstname",
                          @"Bloggs",@"surname",
                          nil],
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Simon",@"firstname",
                          @"Templar",@"surname",
                          nil],
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Amelia",@"firstname",
                          @"Pond",@"surname",
                          nil],
                         nil];
for (NSMutableDictionary *dic in names) {
    dic[@"age"] = @21;
}
NSMutableArray* names = [NSMutableArray arrayWithObjects:
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Joe",@"firstname",
                          @"Bloggs",@"surname",
                          nil],
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Simon",@"firstname",
                          @"Templar",@"surname",
                          nil],
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Amelia",@"firstname",
                          @"Pond",@"surname",
                          nil],
                         nil];
[names enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
    NSMutableDictionary *dic2 = [dic mutableCopy];
    dic2[@"age"] = @21;
    names[idx] = dic2;
}];
请注意,初始数组中的每个元素现在都是
NSMutableDictionary
intsances,而不是
nsdictionanry
。如果由于某种原因不能这样做,那么需要从数组中获取每个字典,将其转换为可变实例,然后用新的字典实例替换相关的数组元素

如果无法为数组创建
NSMutableDictionary
实例,则需要的代码如下所示:

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Joe",@"firstname",
                          @"Bloggs",@"surname",
                          nil],
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Simon",@"firstname",
                          @"Templar",@"surname",
                          nil],
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Amelia",@"firstname",
                          @"Pond",@"surname",
                          nil],
                         nil];
for (NSMutableDictionary *dic in names) {
    dic[@"age"] = @21;
}
NSMutableArray* names = [NSMutableArray arrayWithObjects:
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Joe",@"firstname",
                          @"Bloggs",@"surname",
                          nil],
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Simon",@"firstname",
                          @"Templar",@"surname",
                          nil],
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Amelia",@"firstname",
                          @"Pond",@"surname",
                          nil],
                         nil];
[names enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
    NSMutableDictionary *dic2 = [dic mutableCopy];
    dic2[@"age"] = @21;
    names[idx] = dic2;
}];

如果可以将字典数组定义为可变字典,则可以执行以下操作:

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Joe",@"firstname",
                          @"Bloggs",@"surname",
                          nil],
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Simon",@"firstname",
                          @"Templar",@"surname",
                          nil],
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Amelia",@"firstname",
                          @"Pond",@"surname",
                          nil],
                         nil];
for (NSMutableDictionary *dic in names) {
    dic[@"age"] = @21;
}
NSMutableArray* names = [NSMutableArray arrayWithObjects:
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Joe",@"firstname",
                          @"Bloggs",@"surname",
                          nil],
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Simon",@"firstname",
                          @"Templar",@"surname",
                          nil],
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Amelia",@"firstname",
                          @"Pond",@"surname",
                          nil],
                         nil];
[names enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
    NSMutableDictionary *dic2 = [dic mutableCopy];
    dic2[@"age"] = @21;
    names[idx] = dic2;
}];
请注意,初始数组中的每个元素现在都是
NSMutableDictionary
intsances,而不是
nsdictionanry
。如果由于某种原因不能这样做,那么需要从数组中获取每个字典,将其转换为可变实例,然后用新的字典实例替换相关的数组元素

如果无法为数组创建
NSMutableDictionary
实例,则需要的代码如下所示:

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Joe",@"firstname",
                          @"Bloggs",@"surname",
                          nil],
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Simon",@"firstname",
                          @"Templar",@"surname",
                          nil],
                         [NSMutableDictionary dictionaryWithObjectsAndKeys:
                          @"Amelia",@"firstname",
                          @"Pond",@"surname",
                          nil],
                         nil];
for (NSMutableDictionary *dic in names) {
    dic[@"age"] = @21;
}
NSMutableArray* names = [NSMutableArray arrayWithObjects:
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Joe",@"firstname",
                          @"Bloggs",@"surname",
                          nil],
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Simon",@"firstname",
                          @"Templar",@"surname",
                          nil],
                         [NSDictionary dictionaryWithObjectsAndKeys:
                          @"Amelia",@"firstname",
                          @"Pond",@"surname",
                          nil],
                         nil];
[names enumerateObjectsUsingBlock:^(NSDictionary *dic, NSUInteger idx, BOOL * _Nonnull stop) {
    NSMutableDictionary *dic2 = [dic mutableCopy];
    dic2[@"age"] = @21;
    names[idx] = dic2;
}];

若要在字典中添加键和值,则使用NSMutableDictionary并在字典中添加键和值

下面是代码:

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Joe",@"firstname",
                              @"Bloggs",@"surname",
                              nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Simon",@"firstname",
                              @"Templar",@"surname",
                              nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Amelia",@"firstname",
                              @"Pond",@"surname",
                              nil],
                             nil];
    for (int i=0; i<names.count; i++) {
        NSMutableDictionary *name = [NSMutableDictionary dictionaryWithDictionary:names[i]];
        NSDictionary *test = [NSDictionary dictionaryWithObject:@"Test" forKey:@"Demo"];
        [name addEntriesFromDictionary:test];
        names[i] = name;
    }
    NSLog(@"%@",names);
NSMutableArray*名称=[NSMutableArray arrayWithObjects:
[NSDictionary Dictionary WithObjects和Keys:
@“乔”@“名字”,
@“Bloggs”@“姓氏”,
零],
[NSDictionary Dictionary WithObjects和Keys:
@“西蒙”@“名字”,
@“圣堂武士”@“姓氏”,
零],
[NSDictionary Dictionary WithObjects和Keys:
@“Amelia”@“firstname”,
@“池塘”@“姓氏”,
零],
零];

对于(int i=0;i如果要在字典中添加键和值,则使用NSMutableDictionary并在字典中添加键和值

下面是代码:

NSMutableArray* names = [NSMutableArray arrayWithObjects:
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Joe",@"firstname",
                              @"Bloggs",@"surname",
                              nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Simon",@"firstname",
                              @"Templar",@"surname",
                              nil],
                             [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Amelia",@"firstname",
                              @"Pond",@"surname",
                              nil],
                             nil];
    for (int i=0; i<names.count; i++) {
        NSMutableDictionary *name = [NSMutableDictionary dictionaryWithDictionary:names[i]];
        NSDictionary *test = [NSDictionary dictionaryWithObject:@"Test" forKey:@"Demo"];
        [name addEntriesFromDictionary:test];
        names[i] = name;
    }
    NSLog(@"%@",names);
NSMutableArray*名称=[NSMutableArray arrayWithObjects:
[NSDictionary Dictionary WithObjects和Keys:
@“乔”@“名字”,
@“Bloggs”@“姓氏”,
零],
[NSDictionary Dictionary WithObjects和Keys:
@“西蒙”@“名字”,
@“圣堂武士”@“姓氏”,
零],
[NSDictionary Dictionary WithObjects和Keys:
@“Amelia”@“firstname”,
@“池塘”@“姓氏”,
零],
零];

对于(int i=0;i您可以使用dip链接进行此操作。

以下是示例代码:

NSMutableDictionary mutableCopy = (NSMutableDictionary )CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFDictionaryRef)originalDictionary, kCFPropertyListMutableContainers);

您可以使用dip链接进行此操作。

以下是示例代码:

NSMutableDictionary mutableCopy = (NSMutableDictionary )CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFDictionaryRef)originalDictionary, kCFPropertyListMutableContainers);

您将一个新变量
dic
引入图片中,但我看不出如何更改
名称
对象。顺便说一下,请将json对象转换为
NSDictionary
查看
for
循环,以查看
dic
是如何形成的。代码只是在数组中的每个元素中进行迭代。Si如果您将代码与通过代码创建的数组一起呈现,则上述解决方案将起作用。但是,如果您通过API调用获取数组,则该解决方案将不起作用。首先,您需要将字典转换为我提到的可变字典。您将一个新变量
dic
带入图片中,但我看不出如何更改
名称s
object。顺便说一句,将json对象转换为
NSDictionary
请参见
for
循环,以了解
dic
是如何形成的。代码只是在数组中的每个元素之间进行迭代。由于您使用通过代码创建的数组来呈现代码,因此上述解决方案将起作用。但是如果您通过API调用调用数组,那么这将不起作用。首先,您需要将字典转换为可变字典,如我所述。