将值和键添加到 NSMutableArray - Objc
Adding a value and key into an NSMutableArray - Objc
我已经阅读并尝试了十几个或更多我自己的问题的变体,但仍然需要一些帮助。
我有一个很大的现有数组,我想为每条记录添加一个新对象(键和值)。
这是传入数组中的一个元素:
{
"trip_id": 65,
"arrival_time": "08:56:08",
"departure_time": "08:56:08",
"stop_id": 1161,
"stop_sequence": 8,
"stop_headsign": 0
},
这就是我想要实现的:
{
"trip_id": 65,
"arrival_time": "08:56:08",
"departure_time": "08:56:08",
"stop_id": 1161,
"stop_name": "a stop name",
"stop_sequence": 8,
"stop_headsign": 0
},
到目前为止,这是我的代码——注释行是其他尝试:
NSString *nameKey = @"stop_name";
int i=0;
for (i=0; i<stopTimesArray.count; i++) {
NSNumber *stopTimesId = [stopTimesArray[i] valueForKey:@"stop_id"];
int j=0;
for (j=0; j<stopArray.count; j++) {
NSNumber *stopId = [stopArray[j] valueForKey:@"stop_id"];
if (stopId == stopTimesId) {
NSString *stopNameString = [stopArray[j] valueForKey:@"stop_name"];
NSLog(@"stopNameString: %@", stopNameString);
[outgoingStopTimesDictionary setObject:stopNameString forKey:@"stop_name"];
//[outgoingStopTimesArray addObject:outgoingStopTimesDictionary];
//[outgoingStopTimesArray addObjectsFromArray:stopTimesArray[i]];
//[stopTimesArray[i] addObject:@{@"stop_name":stopNameString}];
//[stopTimesArray[i] addObject:@{@"stop_name":stopNameString}];
[stopTimesArray[i] addObject: outgoingStopTimesDictionary];
}
}
}
//NSLog(@"outgoingStopTimesArray: %@", outgoingStopTimesArray);
//NSLog(@"outgoingStopTimesDictionary: %@", outgoingStopTimesDictionary);
//NSLog(@"stopTimesArray: %@", stopTimesArray);
我遇到的错误是:
stopNameString: S Monroe Street, NB @ 18th Street S, NS
[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x7fd7f2c22760
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x7fd7f2c22760'
当我尝试将字典添加到我的数组时,要么得到一个空字典,要么得到一个无法识别的对象异常。请指出有效的答案,我会删除我的问题。
dictTo=[NSDictionary new];
dictTo =
@{
@"vPLocationLat" : [NSString stringWithFormat:@"%@",[[[[json valueForKey:@"result"] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lat"]],
@"vPLocationLong" : [NSString stringWithFormat:@"%@",[[[[json valueForKey:@"result"] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lng"]]
};
arrLocationList =[NSMutableArray new];
arrLocationList =[dictTo[@"data"] mutableCopy];
另一个解决方案
-> 尝试按照给定的代码实施您的代码
NSMutableArray* newArray = [NSMutableArray array];
NSArray* oldArray = outerDictionary[@"scores"];
for (NSDictionary* dictEntry in oldArray) {
NSString* leagueCode = dictEntry[@"league_code"];
if ([leagueCode isEqualToString @"epl"]) {
[newArray addObject:dictEntry];
}
}
另一种解决方案
尝试这样的事情。
假设您的数组名为 array 并且 yourNewNameString 是 name 的新值
for(NSMutableDictionary *dict in array){
if([[dict objectForKey:@"id"]integerValue]==5){
[dict setObject:yourNewNameString forKey@"name"];
}
}
编辑这是假设您使用 NSMutableDictionarys(不仅仅是 NSDictionarys)初始化数组
//You can create dictionary and add it into NSMutableArray Object Like..
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSDictionary *inventory = @{
@"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
@"Mercedes-Benz E350" : [NSNumber numberWithInt:22],
@"BMW M3 Coupe" : [NSNumber numberWithInt:19],
@"BMW X6" : [NSNumber numberWithInt:16],
};
[arr addObject:inventory];
//You can access using key like...
NSString *strForBMWX6 = [[arr objectAtIndex:0] valueForKey:@"BMW X6"];
// in your case you just miss objectAtIndex:j
看来您的目标是在 stopTimesArray[i]
处的字典中添加一对新的 key/value 对。这是您的代码,所有代码都已根据我认为您需要的内容进行了清理:
for (NSMutableDictionary *stopTimesDictionary in stopTimesArray) {
NSNumber *stopTimesId = stopTimesDictionary[@"stop_id"];
for (NSDictionary *stopDictionary in stopArray) {
NSNumber *stopId = stopDictionary[@"stop_id"];
if ([stopTimesId isEqual:stopId]) {
NSString *stopNameString = stopDictionary[@"stop_name"];
stopTimesDictionary[@"stop_name"] = stopNameString;
// Uncomment the following line if "stop_id" is unique within the "stopArray"
// break;
}
}
}
因为您的 stopTimesArray
似乎包含不可变的字典,所以上面的代码将无法正常工作。这是处理该问题的解决方案:
for (NSInteger i = 0; i < stopTimesArray.count; i++) {
NSDictionary *stopTimeDictionary = stopTimesArray[i];
NSNumber *stopTimesId = stopTimesDictionary[@"stop_id"];
for (NSDictionary *stopDictionary in stopArray) {
NSNumber *stopId = stopDictionary[@"stop_id"];
if ([stopTimesId isEqual:stopId]) {
NSString *stopNameString = stopDictionary[@"stop_name"];
NSMutableDictionary *tempDict = [stopTimesDictionary mutableCopy];
tempDict[@"stop_name"] = stopNameString;
[stopTimesArray replaceObjectAtIndex:i withObject:tempDict];
// Uncomment the following line if "stop_id" is unique within the "stopArray"
// break;
}
}
}
首先感谢@rmaddy。
我稍微修改了一下他的答案,但他的答案基本正确。
我的最终代码如下所示:
for (NSInteger i = 0; i < stopTimesArray.count; i++) {
NSDictionary *stopTimesDictionary = stopTimesArray[i];
NSNumber *stopTimesId = stopTimesDictionary[@"stop_id"];
for (NSDictionary *stopDictionary in stopArray) {
NSNumber *stopId = stopDictionary[@"stop_id"];
if ([stopTimesId isEqual:stopId]) {
NSString *stopNameString = stopDictionary[@"stop_name"];
NSMutableDictionary *tempDict = [stopTimesDictionary mutableCopy];
tempDict[@"stop_name"] = stopNameString;
[outgoingStopTimesArray addObject:tempDict];
break;
}
}
}
我已经阅读并尝试了十几个或更多我自己的问题的变体,但仍然需要一些帮助。
我有一个很大的现有数组,我想为每条记录添加一个新对象(键和值)。
这是传入数组中的一个元素:
{
"trip_id": 65,
"arrival_time": "08:56:08",
"departure_time": "08:56:08",
"stop_id": 1161,
"stop_sequence": 8,
"stop_headsign": 0
},
这就是我想要实现的:
{
"trip_id": 65,
"arrival_time": "08:56:08",
"departure_time": "08:56:08",
"stop_id": 1161,
"stop_name": "a stop name",
"stop_sequence": 8,
"stop_headsign": 0
},
到目前为止,这是我的代码——注释行是其他尝试:
NSString *nameKey = @"stop_name";
int i=0;
for (i=0; i<stopTimesArray.count; i++) {
NSNumber *stopTimesId = [stopTimesArray[i] valueForKey:@"stop_id"];
int j=0;
for (j=0; j<stopArray.count; j++) {
NSNumber *stopId = [stopArray[j] valueForKey:@"stop_id"];
if (stopId == stopTimesId) {
NSString *stopNameString = [stopArray[j] valueForKey:@"stop_name"];
NSLog(@"stopNameString: %@", stopNameString);
[outgoingStopTimesDictionary setObject:stopNameString forKey:@"stop_name"];
//[outgoingStopTimesArray addObject:outgoingStopTimesDictionary];
//[outgoingStopTimesArray addObjectsFromArray:stopTimesArray[i]];
//[stopTimesArray[i] addObject:@{@"stop_name":stopNameString}];
//[stopTimesArray[i] addObject:@{@"stop_name":stopNameString}];
[stopTimesArray[i] addObject: outgoingStopTimesDictionary];
}
}
}
//NSLog(@"outgoingStopTimesArray: %@", outgoingStopTimesArray);
//NSLog(@"outgoingStopTimesDictionary: %@", outgoingStopTimesDictionary);
//NSLog(@"stopTimesArray: %@", stopTimesArray);
我遇到的错误是:
stopNameString: S Monroe Street, NB @ 18th Street S, NS
[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x7fd7f2c22760
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary addObject:]: unrecognized selector sent to instance 0x7fd7f2c22760'
当我尝试将字典添加到我的数组时,要么得到一个空字典,要么得到一个无法识别的对象异常。请指出有效的答案,我会删除我的问题。
dictTo=[NSDictionary new];
dictTo =
@{
@"vPLocationLat" : [NSString stringWithFormat:@"%@",[[[[json valueForKey:@"result"] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lat"]],
@"vPLocationLong" : [NSString stringWithFormat:@"%@",[[[[json valueForKey:@"result"] valueForKey:@"geometry"] valueForKey:@"location"] valueForKey:@"lng"]]
};
arrLocationList =[NSMutableArray new];
arrLocationList =[dictTo[@"data"] mutableCopy];
另一个解决方案
-> 尝试按照给定的代码实施您的代码
NSMutableArray* newArray = [NSMutableArray array];
NSArray* oldArray = outerDictionary[@"scores"];
for (NSDictionary* dictEntry in oldArray) {
NSString* leagueCode = dictEntry[@"league_code"];
if ([leagueCode isEqualToString @"epl"]) {
[newArray addObject:dictEntry];
}
}
另一种解决方案
尝试这样的事情。
假设您的数组名为 array 并且 yourNewNameString 是 name 的新值
for(NSMutableDictionary *dict in array){
if([[dict objectForKey:@"id"]integerValue]==5){
[dict setObject:yourNewNameString forKey@"name"];
}
}
编辑这是假设您使用 NSMutableDictionarys(不仅仅是 NSDictionarys)初始化数组
//You can create dictionary and add it into NSMutableArray Object Like..
NSMutableArray *arr = [[NSMutableArray alloc] init];
NSDictionary *inventory = @{
@"Mercedes-Benz SLK250" : [NSNumber numberWithInt:13],
@"Mercedes-Benz E350" : [NSNumber numberWithInt:22],
@"BMW M3 Coupe" : [NSNumber numberWithInt:19],
@"BMW X6" : [NSNumber numberWithInt:16],
};
[arr addObject:inventory];
//You can access using key like...
NSString *strForBMWX6 = [[arr objectAtIndex:0] valueForKey:@"BMW X6"];
// in your case you just miss objectAtIndex:j
看来您的目标是在 stopTimesArray[i]
处的字典中添加一对新的 key/value 对。这是您的代码,所有代码都已根据我认为您需要的内容进行了清理:
for (NSMutableDictionary *stopTimesDictionary in stopTimesArray) {
NSNumber *stopTimesId = stopTimesDictionary[@"stop_id"];
for (NSDictionary *stopDictionary in stopArray) {
NSNumber *stopId = stopDictionary[@"stop_id"];
if ([stopTimesId isEqual:stopId]) {
NSString *stopNameString = stopDictionary[@"stop_name"];
stopTimesDictionary[@"stop_name"] = stopNameString;
// Uncomment the following line if "stop_id" is unique within the "stopArray"
// break;
}
}
}
因为您的 stopTimesArray
似乎包含不可变的字典,所以上面的代码将无法正常工作。这是处理该问题的解决方案:
for (NSInteger i = 0; i < stopTimesArray.count; i++) {
NSDictionary *stopTimeDictionary = stopTimesArray[i];
NSNumber *stopTimesId = stopTimesDictionary[@"stop_id"];
for (NSDictionary *stopDictionary in stopArray) {
NSNumber *stopId = stopDictionary[@"stop_id"];
if ([stopTimesId isEqual:stopId]) {
NSString *stopNameString = stopDictionary[@"stop_name"];
NSMutableDictionary *tempDict = [stopTimesDictionary mutableCopy];
tempDict[@"stop_name"] = stopNameString;
[stopTimesArray replaceObjectAtIndex:i withObject:tempDict];
// Uncomment the following line if "stop_id" is unique within the "stopArray"
// break;
}
}
}
首先感谢@rmaddy。
我稍微修改了一下他的答案,但他的答案基本正确。
我的最终代码如下所示:
for (NSInteger i = 0; i < stopTimesArray.count; i++) {
NSDictionary *stopTimesDictionary = stopTimesArray[i];
NSNumber *stopTimesId = stopTimesDictionary[@"stop_id"];
for (NSDictionary *stopDictionary in stopArray) {
NSNumber *stopId = stopDictionary[@"stop_id"];
if ([stopTimesId isEqual:stopId]) {
NSString *stopNameString = stopDictionary[@"stop_name"];
NSMutableDictionary *tempDict = [stopTimesDictionary mutableCopy];
tempDict[@"stop_name"] = stopNameString;
[outgoingStopTimesArray addObject:tempDict];
break;
}
}
}