1

I'm pretty new in iOS dev. Basically I have a multidimensional array as per below

Array
(
[0] => Array
  (
  [Name] => Peter
  [Gender] => Male
  )
[1] => Array
  (
  [Name] => Glenn
  [Gender] => Female
  )
[2] => Array
  (
  [Name] => Richard
  [Gender] => Male
  )
)

At some point, I am going to add in additional key/value at certain index. Take for example, I am adding a new entry at index 1 at the end of the array(the sequence is not something to bother actually, it can fit in front or at the end) with [Location] => Japan

As such, the array should looks like this:

Array
(
[0] => Array
  (
  [Name] => Peter
  [Gender] => Male
  )
[1] => Array
  (
  [Name] => Glenn
  [Gender] => Female
  [Location] => Japan
  )
[2] => Array
  (
  [Name] => Richard
  [Gender] => Male
  )
)

How can I achieve that? Pls inspect my code below as I really have no idea as every attempt results in EXC_BAD_ACCESS or app being terminated. Thanks in advance, Jason.

for(int x=0; x<[arrayVisitor count]; x++)
{
  if ([[[arrayVisitor objectAtIndex:x]objectForKey:(@"Gender")]isEqual:@"Female"])
     [[arrayVisitor objectAtIndex:x] addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys: @"Location",@"Japan",nil]];
}
2
  • This is a very strange way of doing things. Why don't you just put a dictionary with multiple keys and values instead of 3 dictionaries each with one key and value? Commented Jun 12, 2013 at 9:58
  • 2
    Before trying to make iOS apps, you should 1. develop some common sense, 2. learn something about algorithms and data types, 3. learn C as well. Until then, there's no point in desperately trying to get things right, you won't succeed if you don't even have the slightest idea about resolving trivial problems like this. Be patient. Commented Jun 12, 2013 at 10:01

1 Answer 1

3

For this type of adding key value pair to array you need to use NSMutableArray and NSMutableDictionary.

NSMutableArray *outerArray=[[NSMutableArray alloc] init];

NSMutableDictionary *mutableDict=[[NSMutableDictionary alloc]initWithDictionary:[outerArray objectAtIndex:1]];
[mutableDict setObject:@"Japan" forKey:@"Location"];

[outerArray replaceObjectAtIndex:1 withObject:mutableDict];

Or using for loop as:------------------

NSMutableArray *arrayVisitor=[[NSMutableArray alloc] init];
int arrayLength=arrayVisitor.count;

for (int i=0;i<arrayLength;i++) {
    NSMutableDictionary *mutableDict=[[NSMutableDictionary alloc]initWithDictionary:[arrayVisitor objectAtIndex:i]];
    if ([[mutableDict valueForKey:@"Gender"] isEqualToString:@"Female"]) {
        [mutableDict setObject:@"Japan" forKey:@"Location"];
        [arrayVisitor replaceObjectAtIndex:i withObject:mutableDict];
    }
}

Note:- It is not multidimensional array , it is Array of NSDictionary objects,It should look like this

Array=(
       {
           Name= Peter
           Gender= Male
       },
       {
           Name = Glenn
           Gender = Female
       },
       {
           Name = Richard
           Gender = Male
       }
     )
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate! You save my day. Ah..Right, it is actually an array of NSDictionary objects! Sorry for my noobiness~ Cheers :)

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.