[英]Keep NSArray original order in NSCountedSet
I have an NSArray
and I need to get data from two keys and put together in a NSMutableDictionary
. 我有一个
NSArray
,我需要从两个键中获取数据并将其放到NSMutableDictionary
。 One key has string
values and the other NSNumber
values. 一个键具有
string
值,另一个键具有NSNumber
值。 When I try to create NSCountedSet
without adding the keys I want to use to separate arrays
, it doesn't work, because the objects are not identical, basically, I need to check if objectId
is identical, don't matter if the other keys are different. 当我尝试创建
NSCountedSet
而不添加要用于分隔arrays
的键时,它不起作用,因为对象不相同,基本上,我需要检查objectId
是否相同,其他键是否无关紧要是不同的。
Here is the initial code: 这是初始代码:
for (PFObject *objeto in objects) {
PFObject *exercicio = objeto[@"exercicio"];
NSString *string = exercicio.objectId;
NSNumber *nota = objeto[@"nota"];
[exercicios addObject:string];
[notas addObject:nota];
So I create two NSMutableArrays
and store the values I need. 因此,我创建了两个
NSMutableArrays
并存储所需的值。 When I log
the arrays after this, they are perfectly ordered, meaning the NSString
is in the same index
of the NSNumber
it belongs to in the other array
. 当我在此之后
log
数组时,它们的排序是完美的,这意味着NSString
与另一个array
所属的NSNumber
处于同一index
中。 So far, so good. 到现在为止还挺好。
Now, when I create the NSCountedSet
with the strings, it changes the order. 现在,当我使用字符串创建
NSCountedSet
时,它将更改顺序。
NSCountedSet *countedExercicios = [[NSCountedSet alloc] initWithArray:exercicios];
. 。
My goal is to sum
the NSNumbers
pertaining to an specific object, therefore, when the order changes, I lose the connection between the two arrays
. 我的目标是
sum
与特定对象有关的NSNumbers
,因此,当顺序更改时,我将失去两个arrays
之间的连接。
I'm not sure what I could do to solve this problem, or even if there's a different approach to achieve the result I need. 我不确定该如何解决这个问题,甚至不确定是否有其他方法可以达到所需的结果。
You can create NSDictionary and add it to array. 您可以创建NSDictionary并将其添加到数组。 You will have just one array and you won't lose the connection, you can use objectId as a key and NSNumber as a value:
您将只有一个数组,并且不会丢失连接,可以将objectId用作键,将NSNumber用作值:
for (PFObject *objeto in objects) {
PFObject *exercicio = objeto[@"exercicio"];
NSString *string = exercicio.objectId;
NSNumber *nota = objeto[@"nota"];
NSDictionary *dict = @{string: nota};
[newArray addObject: dict];
}
When you need get all key (objectId) you can use NSPredictate. 当需要获取所有键(objectId)时,可以使用NSPredictate。
Hope this help 希望这个帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.