我正在使用派生自 Apple 的DateSectionTitles示例代码的代码。在我的课堂Appointment
上,我与Location
. 此外,我生成 UITableViewController 使用的部分标识符。
@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>)
如果我放弃生成节标识符并使用 a@dynamic
而不是@synthesized
属性,它仍然可以工作。这是什么原因,我该如何克服?