0

First post here, so just wanted to say hi. Im a starting iOs developer, just for fun though.

Ive been breaking my head over the following code:

for (int n = 0; n <= iterations; n = n + 1) {

   int interval = [[object valueForKey:@"interval"] integerValue];
   NSTimeInterval singeltonTimestamp = interval * n;
   NSLog(@"%d",(int)singeltonTimestamp);

   [skeleton removeObjectForKey:@"date"];
   [skeleton setObject:[[object objectForKey:@"start"] dateByAddingTimeInterval:singeltonTimestamp] forKey:@"date"];
   [yuups addObject:skeleton];
   NSLog(@"adding skeleton");

}

I have an object called skeleton and I am trying to add 4 of them (iterations = 3) with the date increasing with a certain interval. The singeltonTimestamp changes correctly (reading the NSLog output) but the date's for the skeletons are all the same, they dont increase.

"Object" contains a start date and an interval, I set some stuff (like the title) for the skeleton beforehand.

See this output

014-04-12 14:32:38.676 yuup[8397:60b] (
        {
        date = "2014-04-15 18:02:00 +0000";
        title = test;
    },
        {
        date = "2014-04-15 18:02:00 +0000";
        title = test;
    },
        {
        date = "2014-04-15 18:02:00 +0000";
        title = test;
    },
        {
        date = "2014-04-15 18:02:00 +0000";
        title = test;
}
)

Help or tips are much appriciated. Thanks in advance

Larme
  • 24,190
  • 6
  • 51
  • 81
Scott
  • 99
  • 5

1 Answers1

1

Try this

skeleton = [NSMutableDictionary dictionary];
[skeleton setObject:[[object objectForKey:@"start"] dateByAddingTimeInterval:singeltonTimestamp] forKey:@"date"];
[yuups addObject:skeleton];

You are adding same object again and again.. You have to make new instance before adding to the NSMutableArray

Faisal Ali
  • 1,135
  • 10
  • 17
  • My skeleton is already filled with some data from before the for-loop, so creating the skeleton empty in the loop wont work for me. maybe i should copy it? – Scott Apr 12 '14 at 13:30
  • Exactly you can either copy it or create new, but dont use same object to add in yuups – Faisal Ali Apr 12 '14 at 15:38
  • could you maybe give me a quick code example of how to best copy the object into a new object and then add it to the yuups mutablearray? – Scott Apr 14 '14 at 12:43