gpt4 book ai didi

ios - 如何根据另一个数组中的值向 NSMutableArray 添加索引?

转载 作者:行者123 更新时间:2023-11-28 18:56:38 25 4
gpt4 key购买 nike

我看过很多关于 NS(可变)数组的问题。我想我没有理解这个概念,或者这些问题似乎不相关。

我想做的是:

传入数组 1:

姓名代码开始时间时间结束等等

传入数组 2

代码序数

我想要的:

序数姓名代码开始时间时间结束等等

这是我目前的代码:

int i=0;
for (i=0; i < stationListArray.count; i++) {
NSString *slCodeString = [stationListArray[i] valueForKey:@"Code"];
NSLog(@"slCodeString: %@", slCodeString);
int j=0;
for (j=0; j< lineSequenceArray.count; j++) {
NSString *lsCodeString = [lineSequenceArray[j]valueForKey:@"StationCode"];
NSLog(@"lsCodeString: %@", lsCodeString);
if ([slCodeString isEqualToString:lsCodeString]) {
NSLog(@"match");
NSString *ordinalString = [lineSequenceArray[j] valueForKey:@"SeqNum"];
NSLog(@"ordinalString: %@", ordinalString);
[stationListArray[i] addObject:ordinalString]; <------
}
}
}

我正在记录值,它们正确返回。编译器不喜欢最后一条语句。我收到此错误:

[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x7f9f63e13c30
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x7f9f63e13c30'

这是 StationListArray 的摘录:

(
{
Address = {
City = Greenbelt;
State = MD;
Street = ".....";
Zip = 20740;
};
Code = E10;
Lat = "39.0111458605";
Lon = "-76.9110575731";
Name = Greenbelt;
}
)

最佳答案

        NSString *ordinalString = [lineSequenceArray[j] valueForKey:@"SeqNum"]; //Is NSString
[stationListArray[i] addObject:ordinalString];//<----- trying to call addObject method of NSMutableArray on NSDictionary -> Not GOOD

当你执行 [stationListArray[i] 时,你会得到 NSDictionary(通常它返回一个 NSObject,它位于给定索引处的 NSArray 内,在您的例子中是 NSDictionary)。

因此,为了完成您想要的操作:您应该制作一个 NSMutableDictionary 实例(在这种情况下,它应该是 stationListArray[i] 中的 mutableCopy NSObjectNSDictionary,当您执行 mutableCopy 时,它会复制整个 NSDictionary 并使它可变)对其进行更改,然后将其分配给 stationListArray[i]

例如:

NSMutableDictionary * tempDict = [[stationArray objectAtIndex:i]mutableCopy];//Create a mutable copy of the `NSDictionary` that is inside the `NSArray`
[[tempDict setObject:ordinalString forKey:@"Ordinal"]; //In this line you are adding the Ordinal `NSString` in to the tempDict `NSMutableDictionary` so now you have the desired `NSMutableDictionary`. You can change the key to whatever you wish.
[stationArray replaceObjectAtIndex:i withObject:[tempDict copy]];//Swap the original(old NSDictionary) with the new updated `NSMutableDictionary` I used the copy method in order to replace it with the IMMUTABLE `NSDictionary`

关于ios - 如何根据另一个数组中的值向 NSMutableArray 添加索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31781851/

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