0

I have an object

@interface QuestionViewModel : NSObject

@property (nonatomic, assign) NSInteger questionId;
@property (nonatomic, strong) NSString *questionText;
@property (nonatomic, assign) NSInteger questionNumber;
@property (nonatomic, strong) NSArray *choices;

@property (nonatomic, strong) QuestionViewModel *nextQuestion;
@property (nonatomic, strong) QuestionViewModel *previousQuestion;

@end

I know when I populate this object it is successful and all properties are initialized fully and correctly.

However, when I pass this object like this:

*This is a different class (it is not in the NSObject defined above).

@property (nonatomic, strong) QuestionViewModel *currentQuestion;

- (void)nextQuestion
{
    [self loadQuestion:self.currentQuestion.nextQuestion];
}

- (void)loadQuestion:(QuestionViewModel *)question
{
    self.currentQuestion = question;
    .
    .
    .
}

question.nextQuestion and question.previousQuestion are nil.

Why when I pass this object do the subsequent objects (nextQuestion and previousQuestion) become nil? It seems like the object is doing a shallow copy rather than a deep copy, not sure though.

It seems like there is something foundational that I do not know about.

tentmaking
  • 2,076
  • 4
  • 30
  • 53
  • 1
    The object isn't copied at all. `question` points to a question and this pointer is assigned to `currentQuestion`. Check in `nextQuestion` the values of `self.currentQuestion`, `self.currentQuestion.nextQuestion` and `self.currentQuestion.nextQuestion.nextQuestion`. – Willeke Jan 27 '17 at 15:52
  • self.currentQuestion.nextQuestion.nextQuestion is nil. self.currentQuestion.nextQuestion is valid for the first question in the list, but nil for all other questions. – tentmaking Jan 27 '17 at 15:57
  • I think the fact that you are creating properties (nextQuestion, previousQuestion) of the same kind of the object it self (QuestionViewModel) is creating some recursive problem of some kind. This is kind of confusing as a strategy. May be it is better to store nextQuestion and previousQuestion as separate class instances and update them accordingly in whatever class you instantiate them? – Alex Jan 27 '17 at 19:31
  • 1
    No, storing nextQuestion and previousQuestion as separate class instances isn't a good idea. Pointing to another instance of the same class is ok. Are all questions initialized correctly? Do they point to the next and previous question? – Willeke Jan 29 '17 at 02:52
  • Yeah, I agree with @Willeke, having different class instances for next and previous is not ideal as they can easily become abandoned by the parent question. All the question were initialized correctly and initially they did all have the correct values for next and previous set. It was only when I passed the variable to a method that I lost the data. I ended up storing indexes to the next and previous objects, instead of the actual objects. It works beautifully. – tentmaking Jan 30 '17 at 16:38
  • 1
    I think you didn't set the next and previous question correctly. You don't just lose data. An object doesn't change while its address is passed as parameter. – Willeke Jan 30 '17 at 18:10

2 Answers2

0

I think you need to initialize your sub-classed QuestionViewModel NSObject first. In the QuestionViewModel.m file you can override the init method. Something like this:

- (id)init {
    if((self = [super init])) {
        // Set whatever init parameters you need here
    }
    return self;
}

Then, in the class where you are trying to use this method simply call:

-(void)viewDidLoad {
    QuestionViewModel *currentQuestion = [[QuestionViewModel  alloc] init];
}
Alex
  • 995
  • 12
  • 25
  • I implemented this, but it did not make a difference. The properties are still becoming nil. Good attempt at a fix though. – tentmaking Jan 27 '17 at 14:08
0

I ended up changing the model to more closely reflect a Linked List. It was close as I was storing previous and next objects, but I ended up changing the previous and next properties to store the index of the object, instead of the actual object.

@property (nonatomic, assign) NSInteger nextQuestionIndex;
@property (nonatomic, assign) NSInteger previousQuestionIndex;
tentmaking
  • 2,076
  • 4
  • 30
  • 53