gpt4 book ai didi

objective-c - NSArray 内的 NSDictionary

转载 作者:行者123 更新时间:2023-12-03 17:31:15 25 4
gpt4 key购买 nike

我试图将 1 个字典的不同实例放入一个数组中。共享预期和当前输出。请建议一些东西提前致谢

Program.h

@property (nonatomic,strong)NSMutableArray* array;

Program.m

-- (void)StartFuntion
{
_array = [[NSMutableArray alloc] init];
NSMutableDictionary* dicOne = [[NSMutableDictionary alloc] init];

[dicOne setObject:@"A" forKey:@"1"]; //Adding new
[dicOne setObject:@"B" forKey:@"2"];
[dicOne setObject:@"C" forKey:@"3"];
[dicOne setObject:@"D" forKey:@"4"];
[self addob:dicOne]; //sending dicOne to funtion

[dicOne removeAllObjects]; //Remove objects

[dicOne setObject:@"X" forKey:@"100"]; //Again Adding new objects
[dicOne setObject:@"Y" forKey:@"200"];
[dicOne setObject:@"Z" forKey:@"300"];
[dicOne setObject:@"W" forKey:@"400"];
[self addob:dicOne];
}


-- (void) addob:(NSMutableDictionary*)dic{

[_array addObject:dic];
NSLog(@"_array = %@",_array);

}

当前输出:

  _array = (
{
100 = X;
200 = Y;
300 = Z;
400 = W;
},
{
100 = X;
200 = Y;
300 = Z;
400 = W;
}
)

预期输出:

  _array = (
{
1 = A;
2 = B;
3 = C;
4 = D;
},
{
100 = X;
200 = Y;
300 = Z;
400 = W;
}
)

最佳答案

您无法重复使用dicOne。创建一个新实例,而不是删除其对象。

_array = [[NSMutableArray alloc] init];
NSMutableDictionary* dicOne = [[NSMutableDictionary alloc] init];

[dicOne setObject:@"A" forKey:@"1"]; //Adding new
[dicOne setObject:@"B" forKey:@"2"];
[dicOne setObject:@"C" forKey:@"3"];
[dicOne setObject:@"D" forKey:@"4"];
[self addob:dicOne]; //sending dicOne to funtion

dicOne = [[NSMutableDictionary alloc] init];

[dicOne setObject:@"X" forKey:@"100"]; //Again Adding new objects
[dicOne setObject:@"Y" forKey:@"200"];
[dicOne setObject:@"Z" forKey:@"300"];
[dicOne setObject:@"W" forKey:@"400"];
[self addob:dicOne];

关于objective-c - NSArray 内的 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34149564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com