0

我正在使用派生自 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属性,它仍然可以工作。这是什么原因,我该如何克服?

4

1 回答 1

0

感谢Martin R为我指明了正确的方向,我在代码中发现了问题。

我不知道的是,Core Data 会自动为您生成额外的原始属性:

例如,给定一个具有 firstName 属性的实体,Core Data 会自动生成 firstName、setFirstName:、primitiveFirstName 和 setPrimitiveFirstName:。Core Data 甚至对由 NSManagedObject 表示的实体也是如此。要在调用这些方法时抑制编译器警告,您应该使用 Objective-C 2.0 声明的属性功能,如“声明”中所述。</p>

来源

我没有意识到这一点的事实导致我以错误的方式采用示例代码。它现在对我有用的方式是:

@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
于 2013-07-21T07:29:35.193 回答