将值和键添加到NSMutableArray中 - Objc

时间:2015-08-22 05:37:06

标签: ios objective-c nsmutablearray nsmutabledictionary

我已经阅读并尝试了十几个或更多我自己的问题,但仍需要一些帮助。

我有一个大的现有数组,我想为每条记录添加一个新对象(键和值)。

这是传入数组中的一个元素:

{ "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'

当我尝试将字典添加到我的数组时,我得到一个空字典或一个无法识别的对象异常。请指出一个有效的答案,我将删除我的问题。

4 个答案:

答案 0 :(得分:0)

                 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];

另一种解决方案

- &GT;尝试按照给定的代码实现您的代码

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)初始化您的数组

答案 1 :(得分:0)

//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

答案 2 :(得分:0)

您的目标似乎是在stopTimesArray[i]向字典添加一个新的键/值对。在这里,您的代码全部用我认为您需要的东西进行清理:

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;
        }
    }
}

答案 3 :(得分:0)

首先,谢谢@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;
            }
        }
    }
相关问题