我正在跟随《Mac OS X下的Cocoa编程》第四版指南。在第32章中,我搞砸了代码,并且没有备份,所以我不得不从Big Nerd Ranch下载解决方案。
该项目涉及核心数据关系,有一个Employee类和一个Department类。
Employee.h:
我有这些核心数据关系和实体:
完整的项目可以在这里找到,第32章(与我的相同)在“下载解决方案”中。 如果您需要更多的代码来理解这个错误,请告诉我。
似乎不推荐发布整个项目,但如果您认为可以的话,我可以这样做。
附注:我正在使用从解决方案中下载的代码,它是相同的,但不起作用,可能是因为我有更高版本的Xcode。
该项目涉及核心数据关系,有一个Employee类和一个Department类。
Employee.h:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Department;
@interface Employee : NSManagedObject
@property (nonatomic, copy) NSString * firstName;
@property (nonatomic, copy) NSString * lastName;
@property (nonatomic, retain) Department *department;
@property (nonatomic, readonly) NSString *fullName;
@end
Employee.m:
#import "Employee.h"
#import "Department.h"
@implementation Employee
@dynamic firstName;
@dynamic lastName;
@dynamic department;
+ (NSSet *)keyPathsForValuesAffectingFullName
{
return [NSSet setWithObjects:@"firstName", @"lastName", nil];
}
- (NSString *)fullName
{
NSString *first = [self firstName];
NSString *last = [self lastName];
if (!first)
return last;
if (!last)
return first;
return [NSString stringWithFormat:@"%@ %@", first, last];
}
@end
Department.h:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Department : NSManagedObject
@property (nonatomic, retain) NSString * deptName;
@property (nonatomic, retain) NSSet *employees;
@property (nonatomic, retain) NSManagedObject *manager;
@end
@interface Department (CoreDataGeneratedAccessors)
- (void)addEmployeesObject:(NSManagedObject *)value;
- (void)removeEmployeesObject:(NSManagedObject *)value;
- (void)addEmployees:(NSSet *)values;
- (void)removeEmployees:(NSSet *)values;
@end
Department.m:
#import "Department.h"
#import "Employee.h"
@implementation Department
@dynamic deptName;
@dynamic employees;
@dynamic manager;
- (void)addEmployeesObject:(Employee *)value
{
NSLog(@"Dept %@ adding employee %@",
[self deptName], [value fullName]);
NSSet *s = [NSSet setWithObject:value];
[self willChangeValueForKey:@"employees"
withSetMutation:NSKeyValueUnionSetMutation
usingObjects:s];
[[self primitiveValueForKey:@"employees"] addObject:value];
[self didChangeValueForKey:@"employees"
withSetMutation:NSKeyValueUnionSetMutation
usingObjects:s];
}
- (void)removeEmployeesObject:(Employee *)value
{
NSLog(@"Dept %@ removing employee %@",
[self deptName], [value fullName]);
Employee *manager = (Employee *)[self manager];
if (manager == value) {
[self setManager:nil];
}
NSSet *s = [NSSet setWithObject:value];
[self willChangeValueForKey:@"employees"
withSetMutation:NSKeyValueMinusSetMutation
usingObjects:s];
[[self primitiveValueForKey:@"employees"] removeObject:value];
[self didChangeValueForKey:@"employees"
withSetMutation:NSKeyValueMinusSetMutation
usingObjects:s];
}
@end
我有这些核心数据关系和实体:
完整的项目可以在这里找到,第32章(与我的相同)在“下载解决方案”中。 如果您需要更多的代码来理解这个错误,请告诉我。
2012-10-31 15:27:43.977 Departments[1229:303] Cannot create BOOL from object (
) of class _NSControllerArrayProxy
2012-10-31 15:27:43.989 Departments[1229:303] Cannot create BOOL from object (
) of class _NSControllerArrayProxy
似乎不推荐发布整个项目,但如果您认为可以的话,我可以这样做。
附注:我正在使用从解决方案中下载的代码,它是相同的,但不起作用,可能是因为我有更高版本的Xcode。