cheesey
cheesey

Reputation: 516

Adding data to an NSDictionary that is within another NSDictionary

I am trying to add data to an NSMutableDictionary that is nested inside other Dictionaries.

Start Output before the code that adds data is run

DayData Dictionary {            //DayData Dictionary

Monday =     {                  //events Dictionary
    trip1 =         {           //eventData Dictionary
        numItems = 9items;
        time = "5:00";
        tripName = beachfucking;
    };
};
Thursday =     {
    trip1 =         {
        numItems = 9items;
        time = "5:00";
        tripName = beachfucking;
    };
};
Tuesday =     {
    trip1 =         {
        numItems = 9items;
        time = "5:00";
        tripName = beachfucking;
    };
};
Wendsday =     {
    trip1 =         {
        numItems = 9items;
        time = "5:00";
        tripName = beachfucking;
    };
};
}

The code generating this output

NSMutableDictionary *eventData = [[NSMutableDictionary alloc]initWithObjects:@[@"5:00",@"9items",@"beachfucking"] forKeys:@[@"time",@"numItems",@"tripName"]];

NSMutableDictionary *event = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                              eventData,@"trip1",nil];

NSMutableDictionary *dayData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        event,@"Monday",event,@"Tuesday",
                                        event,@"Wendsday",event,@"Thursday",nil];

My current try to fix the problem. Here I create a new Event NSDictionary then add that data to the DayData Dictionary. But the output is not correct. Every day of the week gets a "trip2" data set when Monday is only suppose to.

 NSMutableDictionary *event2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                              eventData,@"trip2",nil];
[[dayData objectForKey:@"Monday"] addEntriesFromDictionary:event2];

Current wrong output from code above

2016-12-24 01:56:41.261329 test1[10098:380349] {
    Monday =     {
        trip1 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
        trip2 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
    };
    Thursday =     {
        trip1 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
        trip2 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
    };
    Tuesday =     {
        trip1 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
        trip2 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
    };
    Wendsday =     {
        trip1 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
        trip2 =         {
            numItems = 9items;
            time = "5:00";
            tripName = beachfucking;
        };
    };
    }

Notice how trip2 is filled in for all days of the week.

Upvotes: 0

Views: 112

Answers (1)

Nirav D
Nirav D

Reputation: 72410

Your all days key are contains same reference object event, so that changing one of it, will reflects to all the days key.

To solved the problem declare your dayData Dictionary like this.

NSMutableDictionary *dayData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                [event mutableCopy] ,@"Monday",[event mutableCopy],@"Tuesday",
                                [event mutableCopy],@"Wendsday",[event mutableCopy],@"Thursday",nil];

Upvotes: 2

Related Questions