我正在遵循 cocoa programming for mac os x,第 4 版指南。在第 32 章中,我搞砸了代码,而且我没有备份它,所以我不得不从 big nerd ranch 下载解决方案。
该项目是关于核心数据关系的,有一个类Employee和一个类Department。
雇员.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
员工.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
部门.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
部门.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
我有那些核心数据关系和实体:
我还使用一些数组 Controller 将核心数据值绑定(bind)到一些 socket 。
可以找到完整的项目 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:我用的是从solutions下载的代码,一样但是不行,可能是我的xcode版本高了
最佳答案
看起来像是绑定(bind)问题。您可能有一个“启用”或“隐藏”绑定(bind)(采用 BOOL)与无法转换为 BOOL 的属性相关联。
关于objective-c - 无法从对象创建 BOOL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13160225/