2つのエンティティが1対多の関係にあるコアデータモデルを設定しました(アイテム。アイテムごとに、複数のResetDateが存在する可能性があります)。モデルが正しく設定されていると確信しています。
新しいアイテムを追加できます。その場合、新しいResetDateを追加します(現在の日付を使用して[NSDate date]
)。アイテムを取得して表示できます。私が問題を抱えているのは、ResetDatesを取得して表示することです。
更新:以下の回答者に感謝します。問題のコードは次のとおりです。
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"resetDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
NSMutableArray *sortedResets = [[NSMutableArray alloc] initWithArray:[item.resets allObjects]];
[sortedResets sortUsingDescriptors:sortDescriptors];
NSDate *oldDate = [[sortedResets lastObject] resetDate];
if ( !oldDate ) {
oldDate = [NSDate date];
}
NSInteger numberOfDays = [self timeIntervalWithStartDate:oldDate withEndDate:currentDate]; // This function works fine, when given two NSDate objects
daysSinceLabel.text = [NSString stringWithFormat:@"%d days", numberOfDays];