M
M
Maxim Prigozhenkov2015-02-22 00:08:28
Cocoa
Maxim Prigozhenkov, 2015-02-22 00:08:28

Why is the data being reset in the array?

Hello everyone))
I have written a method:

-(NSArray*)creatingMultipleCards:(NSArray*)array{
    NSMutableArray *tmpCards = [NSMutableArray new];
    
    //Будем проверять каждую карточку на содержание нескольких дат. В случае если дат больше одной, то формируем несколько карточек.
    for(Card *obj in array){
        
        if ([obj.urgencyTimes count] > 1) {
            //Если у карточки больше одной даты, то формируем для каждой даты отдельную карточку.
            for (UrgencyTime *cardTime in obj.urgencyTimes) {
                    Card *tmp = obj;
                    tmp.beginDate = cardTime.start;
                    tmp.endDate = cardTime.end;
                    
                    [tmpCards addObject:tmp];
                }
            }
        else
            {
                [tmpCards addObject:obj];
            }
        }
    
    return tmpCards;
}

There is a card object that contains 5 properties:
  • Event Name
  • Description of the event
  • Event start
  • End of event
  • An array of dates for an event

The method receives an array of cards.
Based on the fact that if in a particular card, the array of dates is greater than one, then for each date the same card is formed as the main one, only with other dates taken from the array.
So, after the cycle that creates these additional cards is completed, all the cards have one start and end date for the event.
And the dates become the same in all cards, only after exiting the cycle.
What could be the problem?
PS Fixed a typo in the code.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
An, 2015-02-22
@Flanker_4

You need to replace
Card *tmp = obj;
On
Card *tmp = [obj copy]
or
Card *tmp = [Card new];
Depending on the specifics of what you're doing,
I can't see all the properties of the Card

A
Alexander Shcherbakov, 2015-03-05
@mkll

To what was said by the previous speaker, I will explain why you need to do what he advised.
Here is your code:

for (UrgencyTime *cardTime in obj.urgencyTimes) {
                    Card *tmp = obj;
                    tmp.beginDate = cardTime.start;
                    tmp.endDate = cardTime.end;
}

What is he doing? Line by line:
0. Well, loop
1. You declare a pointer tmp to an object of type Card and assign to this pointer a reference to an object obj, so you now have tmp == obj and, working with tmp, you are actually working with obj
2 and 3. Assign properties of the obj object some values
​​In the next iteration, you repeat the same thing, only the values ​​assigned are taken from another cardTime object - but they are assigned to the same obj object as in the first iteration.
Then, at a higher level of the loop, you will select another object to which the obj pointer will refer (the pointer is the same, but the object it points to is different), and do the same with it.
You need to smoke pointers and work with them, it is clear from your question that you do not understand them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question