Reputation: 7778
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;
}
)
Upvotes: 0
Views: 84
Reputation: 3512
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`
Upvotes: 3
Reputation: 15
[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];
Upvotes: 1