を渡すとき、私は私はこのオブジェクトを移入するとき、それは成功し、すべてのプロパティが完全にかつ正しく初期化されている知っているオブジェクトデータ損失オブジェクト
@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
を持っています。しかしながら
、私はこのようにこのオブジェクトを渡す(これは、上記で定義NSObjectのではない)。これは、異なるクラスである*
。
@property (nonatomic, strong) QuestionViewModel *currentQuestion;
- (void)nextQuestion
{
[self loadQuestion:self.currentQuestion.nextQuestion];
}
- (void)loadQuestion:(QuestionViewModel *)question
{
self.currentQuestion = question;
.
.
.
}
question.nextQuestion
とquestion.previousQuestion
はnil
です。
なぜこのオブジェクトを渡すと、後続のオブジェクト(nextQuestionおよびpreviousQuestion)がゼロになるのですか?オブジェクトは深いコピーではなく、浅いコピーをしているようですが、確かにそうは思いません。
私には分かっていない何かがあるようです。
オブジェクトはまったくコピーされません。 'question'は質問を指し、このポインタは' currentQuestion'に割り当てられます。 'nextQuestion'で' self.currentQuestion'、 'self.currentQuestion.nextQuestion'と' self.currentQuestion.nextQuestion.nextQuestion'の値をチェックしてください。 – Willeke
self.currentQuestion.nextQuestion.next質問はありません。 self.currentQuestion.nextQuestionはリストの最初の質問に対して有効ですが、他のすべての質問ではnilです。 – tentmaking
私はあなた自身(QuestionViewModel)のオブジェクトの同じ種類のプロパティ(nextQuestion、previousQuestion)を作成しているという事実は、ある種の再帰的な問題を作り出していると思います。これは、戦略として混乱するようなものです。 nextQuestionとpreviousQuestionを別々のクラスインスタンスとして保存し、それらをインスタンス化するどのクラスでもそれに応じて更新するほうが良いでしょうか? – Alex