2012-10-31 42 views
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和實體:

Department

Employee

我也使用一些陣列控制器到核心數據值綁定到一些出口。
完整的項目可以在「下載解決方案」中找到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。

回答

10

看起來像一個綁定問題。你可能有一個「啓用」或「隱藏」的綁定(需要一個BOOL)與一個無法轉換爲BOOL的屬性鏈接。

+0

我仍然無法找到問題,使用返回BOOL的方法所做的唯一連接與數組控制器的canRemove方法連接。如何發佈xib文件? –

+1

也許有助於知道NSControllerArrayProxy對象通常是NSArrayController的contentArray。 – diederikh

+0

以下是一些綁定:Depts數組控制器的託管對象上下文與文件所有者綁定,模型關鍵路徑managedObjectContext.ManagerPopUp的內容集與Depts selection.employees綁定; EmployeeList數組控制器的內容集合與Depts selections.employees綁定; –