我使用的是源自Apple的DateSectionTitles示例代碼的代碼。在我的班級Appointment
我與Location
有關係。此外,我生成一個由UITableViewController使用的部分標識符。NSManagedObject的@synthesize屬性在修改關係後仍然存在錯誤
@class Location;
@interface Appointment : NSManagedObject
@property (nonatomic, retain) NSDate * begin;
@property (nonatomic, retain) NSDate * end;
@property (nonatomic, retain) Location * location;
@property (nonatomic, retain) NSString *sectionIdentifier;
@property (nonatomic, retain) NSString *primitiveSectionIdentifier;
@end
@implementation Appointment
@synthesize begin = _begin;
@dynamic end;
@dynamic location;
@dynamic primitiveSectionIdentifier;
@dynamic sectionIdentifier;
#pragma mark -
#pragma mark Transient properties
- (NSString *)sectionIdentifier {
// Create and cache the section identifier on demand.
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveSectionIdentifier];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp) {
/*
Sections are organized by month and year. Create the section identifier as a string representing the number (year * 1000) + month; this way they will be correctly ordered chronologically regardless of the actual name of the month.
*/
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:[self begin]];
tmp = [NSString stringWithFormat:@"%d", ([components year] * 10000) + [components month] * 100 + [components day]];
[self setPrimitiveSectionIdentifier:tmp];
}
return tmp;
}
#pragma mark -
#pragma mark Begin setter
- (void)setBegin:(NSDate *)begin
{
// If the time stamp changes, the section identifier become invalid.
[self willChangeValueForKey:@"begin"];
[self willChangeValueForKey:@"primitiveSectionIdentifier"];
_begin = begin;
[self setPrimitiveSectionIdentifier:nil];
[self didChangeValueForKey:@"begin"];
[self didChangeValueForKey:@"primitiveSectionIdentifier"];
}
@end
問題:改變位置後的數據撐故障。 之前修改location
物體看起來是這樣的:
<Appointment: 0x837d570> (entity: Appointment; id: 0x837c900 <x-coredata://83B2187C-00B3-4029-B4C5-4EB69C18FC59/Appointment/p1> ; data: {
begin = "2013-07-27 16:00:00 +0000";
end = "2013-07-27 18:00:00 +0000";
location = "0x837e6c0 <x-coredata://83B2187C-00B3-4029-B4C5-4EB69C18FC59/Location/p1>";
})
改變屬性location
後:
<Appointment: 0x9b7b1f0> (entity: Appointment; id: 0x9b7ab50 <x-coredata://83B2187C-00B3-4029-B4C5-4EB69C18FC59/Appointment/p1> ; data: <fault>)
如果我從產生段標識和使用@dynamic
代替@synthesized
財產的放棄它仍然有效。這是什麼原因以及如何克服這個問題?
「約會」與「ReservationModel」相同嗎? –
對不起,由於簡化我的錯。 – Norbert
你是什麼意思的「改變後的位置數據保持故障」?故障不是一個錯誤。什麼不工作? - 還要注意,被管理對象的屬性不是由實例變量實現的。 '@synthesize begin = _begin;'和'_begin = begin;'不正確。 –