0

I have 5 animal objects and i trying to save it in a NSMutableDIctionary. However, according to my code below, only the last animal gets saved in the NSMutableDIctionary.

I don't think i am looping it correctly.

for(int i = 0; i < animalCount; i++) {

        [allContacts setObject:name forKey:@"name"];

        [allContacts setObject:age forKey:@"age"];

        [allContacts setObject:gender forKey:@"gender"];

 }

The output of the above displays as follows: (Only the last animal object gets displayed)

{

    name = fish;

    age = 6;

    gender = "female";

}

How i want is that, all five animal objects to be saved in the NSMutableDIctionary so i can display them in a UITableView.

{

            name = "cow";

            age = 4;

            gender = "male";

        },

                {

            name = "dog";

            age = 15;

            gender = "male";

        },

                {

            name = "fish";

            age = 6;

            gender = "female";

        }

5 Answers 5

4

Key of NSMutableDictionary is unique.

What you want do is build an Array and every object of this Array is a Dictionary

    NSMutableArray * contentsArray = [[NSMutableArray alloc] init];
    for(int i = 0; i < animalCount; i++) {
        NSMutableDictionary * allContacts = [[NSMutableDictionary alloc] init];
        [allContacts setObject:name forKey:@"name"];

        [allContacts setObject:age forKey:@"age"];

        [allContacts setObject:gender forKey:@"gender"];
        [contentsArray addObject:allContacts];
    }
Sign up to request clarification or add additional context in comments.

Comments

2

First create a NSMutableArray in order to loop and create the dictionaries. Then create individual dictionaries.

NSMutableArray *outputArray = [NSMutableArray array];

for ( int i=0; i< animalCount; i++) {
    NSMutableDictionary *internalDictionary = [NSMutableDictionary dictionary];
    [internalDictionary setObject:[nameArray objectAtIndex: i] forKey:@"name"];

    [internalDictionary setObject:[ageArray objectAtIndex: i] forKey:@"age"];

    [internalDictionary setObject:[genderArray objectAtIndex: i] forKey:@"gender"];

    [outputArray addObject:internalDictionary];

}

NSLog(@"%@",outputArray);

Cheers!

Comments

1

You need an array of dictionaries, where your keys are constants (name, age, gender) and your values should be considered from their index (you'll need an array of names, ages, and genders), unless you'll have a lot of similar dictionaries:

NSMutableArray * contentsArray = [NSMutableArray new];
for(int i = 0; i < animalCount; i++) {
    NSMutableDictionary * allContacts = [[NSMutableDictionary alloc] init];

    [allContacts setObject:arrNames[i]   forKey:@"name"];
    [allContacts setObject:arrAges[i]    forKey:@"age"];
    [allContacts setObject:arrGenders[i] forKey:@"gender"];

    [contentsArray addObject:allContacts];
}

or in modern objective c (simpler & shorter):

NSMutableArray *contentsArray = [NSMutableArray new];
for(int i = 0; i < animalCount; i++) {
    [contentsArray addObject:@{@"name"   : arrNames[i],
                               @"age"    : arrAges[i],
                               @"gender" : arrGenders[i]}];
}

Comments

1

The reason is every time you are looping, you are updating the keys of your MSMutableDictionary, but you are not saving it anywhere.

Just take an NSMutableArray and save your dictionaries every time you update it.

So, first initialise the array outside the loop-

NSMutableArray *allContactsArray = [[NSMutableArray alloc] initWithCapacity: animalCount];

Now, run the loop, and save the dictionary every time before you loose it.

for(int i = 0; i < animalCount; i++) {
        [allContacts setValue:name forKey:@"name"];
        [allContacts setValue:age forKey:@"age"];
        [allContacts setValue:gender forKey:@"gender"];

        //save the allContacts dictionary in the array
        [allContactsArray insertObject:allContacts atIndex:i];
    }

This way you also you at what index you are saving your dictionary while adding.

Comments

0

I think you are mistaken. You should be saving dictionaries into an array, to populate tableview.

NSMutableArray *animals = [NSMutableArray array];

for(int i = 0; i < animalCount; i++) {

NSDictionary *dict = @{@"name" : name, @"age" : age, @"gender" : gender};
[animals addObject:dict];

 }

Access in cellForRowAtIndexPath using:

NSDictionary *dict = animals[indexPath.row];
cell.textLabel.text = dict[@"name"];

Comments

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.