3
我正在按照mac os x第4版的指导cocoa编程。在第32章中,我弄糟了代码,而且我没有对它进行备份,所以我不得不从下载大的解决方案书呆子牧场。
该项目是关于核心数据关系的,有一个Employee类和一个Class类。无法从对象创建BOOL
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
我有这些核心数据relashionships和实体:
我也使用一些阵列控制器到核心数据值绑定到一些出口。
完整的项目可以在“下载解决方案”中找到here。,第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
好像张贴整个项目是不推荐,但我可以这样做,如果你说这是好的。 PS:我使用从解决方案下载的代码,它是一样的,但它不起作用,也许是因为我有更高版本的xcode。
我仍然无法找到问题,使用返回BOOL的方法所做的唯一连接与数组控制器的canRemove方法连接。如何发布xib文件? –
也许有助于知道NSControllerArrayProxy对象通常是NSArrayController的contentArray。 – diederikh
以下是一些绑定:Depts数组控制器的托管对象上下文与文件所有者绑定,模型关键路径managedObjectContext.ManagerPopUp的内容集与Depts selection.employees绑定; EmployeeList数组控制器的内容集合与Depts selections.employees绑定; –