0

を渡すとき、私は私はこのオブジェクトを移入するとき、それは成功し、すべてのプロパティが完全にかつ正しく初期化されている知っているオブジェクトデータ損失オブジェクト

@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.nextQuestionquestion.previousQuestionnilです。

なぜこのオブジェクトを渡すと、後続のオブジェクト(nextQuestionおよびpreviousQuestion)がゼロになるのですか?オブジェクトは深いコピーではなく、浅いコピーをしているようですが、確かにそうは思いません。

私には分かっていない何かがあるようです。

+1

オブジェクトはまったくコピーされません。 'question'は質問を指し、このポインタは' currentQuestion'に割り当てられます。 'nextQuestion'で' self.currentQuestion'、 'self.currentQuestion.nextQuestion'と' self.currentQuestion.nextQuestion.nextQuestion'の値をチェックしてください。 – Willeke

+0

self.currentQuestion.nextQuestion.next質問はありません。 self.currentQuestion.nextQuestionはリストの最初の質問に対して有効ですが、他のすべての質問ではnilです。 – tentmaking

+0

私はあなた自身(QuestionViewModel)のオブジェクトの同じ種類のプロパティ(nextQuestion、previousQuestion)を作成しているという事実は、ある種の再帰的な問題を作り出していると思います。これは、戦略として混乱するようなものです。 nextQuestionとpreviousQuestionを別々のクラスインスタンスとして保存し、それらをインスタンス化するどのクラスでもそれに応じて更新するほうが良いでしょうか? – Alex

答えて

0

サブクラスQuestionViewModel NSObjectを最初に初期化する必要があると思います。 QuestionViewModel.mファイルでは、initメソッドをオーバーライドすることができます。このような何か:

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

その後、あなたは、単に呼び出し、このメソッドを使用しようとしているクラスで:

-(void)viewDidLoad { 
    QuestionViewModel *currentQuestion = [[QuestionViewModel alloc] init]; 
} 
+0

私はこれを実装しましたが、違いはありませんでした。プロパティはまだゼロになっています。しかし、修正の良い試み。 – tentmaking

0

私はより密接にリンクされたリストを反映するためにモデルを変更することになりました。私は前と次のオブジェクトを保存していたので近いですが、実際のオブジェクトではなく、オブジェクトのインデックスを格納する前と次のプロパティを変更しました。

@property (nonatomic, assign) NSInteger nextQuestionIndex; 
@property (nonatomic, assign) NSInteger previousQuestionIndex; 
+0

アドレスを保存しました。 'QuestionViewModel *'はポインタです。 – Willeke