我有這個簡單的核心數據模型:當我將新的託管對象添加到關係集時,爲什麼我的應用崩潰了?
問題,答案
每一個每一個問題都有4個答案。
的代碼如下: Question.m
@interface Question : NSManagedObject
{
}
@property (nonatomic, retain) NSString * questionText;
@property (nonatomic, retain) NSSet* answers;
@property (nonatomic, retain) Package * package;
@end
@interface Question (CoreDataGeneratedAccessors)
- (void)addAnswersObject:(NSManagedObject *)value;
- (void)removeAnswersObject:(NSManagedObject *)value;
- (void)addAnswers:(NSSet *)value;
- (void)removeAnswers:(NSSet *)value;
@end
Answer.m
@class Question;
@interface Answer : NSManagedObject
{
}
@property (nonatomic, retain) NSString * answerText;
@property (nonatomic, retain) NSNumber * correct;
@property (nonatomic, retain) Question * question;
@end
問題是,當我嘗試回答添加到與addAnswersObject的問題。
這是崩潰的應用程序的代碼的一部分:
for (CXMLElement *theElement in theNodes)
{
Question *qst = [NSEntityDescription insertNewObjectForEntityForName:@"Question" inManagedObjectContext:moc];
// Create a counter variable as type "int"
int counter;
// Loop through the children of the current node
for(counter = 0; counter < [theElement childCount]; counter++) {
if([[[theElement childAtIndex:counter] name] isEqualToString: @"question"])
[qst setQuestionText:[[theElement childAtIndex:counter] stringValue]];
if([[[theElement childAtIndex:counter] name] isEqualToString: @"answer"]) {
Answer *answer = [NSEntityDescription insertNewObjectForEntityForName:@"Answer" inManagedObjectContext:moc];
[answer setAnswerText:[[theElement childAtIndex:counter] stringValue]];
CXMLElement *answerElement = (CXMLElement *)[theElement childAtIndex:counter];
if([[[answerElement attributeForName:@"correct"] stringValue] isEqualToString:@"YES"]) {
[answer setCorrect:[NSNumber numberWithBool:YES]];
} else {
[answer setCorrect:[NSNumber numberWithBool:NO]];
}
[qst addAnswersObject:answer]; //The app crash here
}
}
這是日誌從控制檯:
2010-05-24 20:02:38.475 Fgq [5670: 40b] *** - [NSUserDefaults objectForKey:]:發送到釋放實例的消息 0x3c179a0程序接收信號: 「EXC_BAD_ACCESS」。
我從對象數據模型中多次重新導出所有對象而沒有成功,我檢查了所有關係,看起來一切正常。
會出現什麼樣的問題?
增加了我所報告的... – bontoJR 2010-05-24 21:39:01