2013-07-20 28 views
0

我使用的是源自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财产的放弃它仍然有效。这是什么原因以及如何克服这个问题?

+0

“约会”与“ReservationModel”相同吗? –

+0

对不起,由于简化我的错。 – Norbert

+0

你是什么意思的“改变后的位置数据保持故障”?故障不是一个错误。什么不工作? - 还要注意,被管理对象的属性不是由实例变量实现的。 '@synthesize begin = _begin;'和'_begin = begin;'不正确。 –

回答

0

感谢Martin R谁指出我在正确的方向我发现我的代码中的问题。

什么我不知道的是,核心数据自动生成额外原始属性你的事实:给出一个属性名字,核心数据 自动生成的firstName实体

例如, setFirstName:,primitiveFirstName, 和setPrimitiveFirstName :.核心数据即使对于由NSManagedObject表示的实体 也这样做。为了抑制编译器警告,当你 调用这些方法,你应该使用Objective-C 2.0宣告 属性功能,如“宣言”。

Source

说我没”事实意识到这一点,导致我以错误的方式采用示例代码。它对我的工作方式是:

@class Location; 

@interface Appointment : NSManagedObject 

@property (nonatomic, retain) NSDate * primitiveBegin; 
@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 

@dynamic primitiveBegin; 
@dynamic 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 setPrimitiveBegin:begin]; 
    [self didChangeValueForKey:@"begin"]; 

    [self setPrimitiveSectionIdentifier:nil]; 
} 


#pragma mark - 
#pragma mark Key path dependencies 

+ (NSSet *)keyPathsForValuesAffectingSectionIdentifier 
{ 
    // If the value of timeStamp changes, the section identifier may change as well. 
    return [NSSet setWithObject:@"begin"]; 
} 

@end 
相关问题