0

I've looked at lots of questions about NS(Mutable)Arrays. I guess I am not grokking the concept, or the questions don't seem relevant.

What I' trying to do is the following:

Incoming Array 1:

Name Code Start time End Time etc

Incoming Array 2

Code Ordinal

What I want:

Ordinal Name Code Start time End Time etc

This is my code at present:

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

I'm logging the values and they return correctly. The compiler doesn't like the last statement. I get this error:

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

Here is an excerpt from the StationListArray:

(
        {
        Address =         {
            City = Greenbelt;
            State = MD;
            Street = ".....";
            Zip = 20740;
        };
        Code = E10;
        Lat = "39.0111458605";
        Lon = "-76.9110575731";
        Name = Greenbelt;
    }   
)
11
  • 2
    Apparently stationListArray[i] is a NSDictionary, not a NSArray. Commented Aug 3, 2015 at 7:41
  • 1
    Probably you want to use the insertObject:atIndex: method documented in developer.apple.com/library/mac/documentation/Cocoa/Reference/… Commented Aug 3, 2015 at 7:46
  • @Larme. I've added an excerpt from the array to my question. Commented Aug 3, 2015 at 7:48
  • 1
    So I was right ? you have an array1 and array2 and you want to insert in array1 the values of array2 ? Commented Aug 3, 2015 at 7:56
  • 1
    yes. I'd prefer only the ordinal object but merging the arrays would work Commented Aug 3, 2015 at 7:57

2 Answers 2

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

When you do [stationListArray[i] you get NSDictionary in your case (Generally it returns an NSObject that is inside the NSArray at the given index, in your case is NSDictionary).

So in order to complete your desired operation: you should make an NSMutableDictionary instance (In this case it should be the mutableCopy from the stationListArray[i]'s NSObject which is NSDictionary, when you do mutableCopy it copies the entire NSDictionary and makes it Mutable) make the changes on it and then assign it in to the stationListArray[i]

For example:

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`
Sign up to request clarification or add additional context in comments.

2 Comments

great. Thanks very much.
@DavidDelMonte You are welcome! I added some comments to explain the code snippet I hope it is clear enough.
1
            [stationListArray[i] addObject:ordinalString]; <------

This is not an NSMutableArray. You must use

            [stationListArray addObject:ordinalString]; <------

instead of the what you have done.

Here is the way you can write a better understandable code because for me the code is not clear.You can also try like this in loop to achieve what you want.

NSMutableArray *array = [NSMutableArray new];
NSMutableDictionary *dictMain = [NSMutableDictionary new];
NSMutableDictionary *dictAddress = [NSMutableDictionary new];

[dictAddress setValue:@"Greenbelt" forKey:@"City"];
[dictAddress setValue:@"MD" forKey:@"State"];
[dictAddress setValue:@"....." forKey:@"Street"];
[dictAddress setValue:@"20740" forKey:@"Zip"];

[dictMain setValue:dictAddress forKey:@"Address"];
[dictMain setValue:@"E10" forKey:@"Code"];
[dictMain setValue:@"39.0111458605" forKey:@"Lat"];
[dictMain setValue:@"-76.9110575731" forKey:@"Lon"];
[dictMain setValue:@"Greenbelt" forKey:@"Name"];

[array addObject:dictMain];

1 Comment

hmm. I'm loping through stationListArray to perform this function a number [i] of times..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.