Ray's playground

 

Core Data Relationships(Chapter 30 of Cocoa Programming for Mac OS X)

 1 #import "Department.h"
 2 #import "Employee.h"
 3 
 4 @implementation Department
 5 @dynamic deptName;
 6 @dynamic employees;
 7 @dynamic manager;
 8 
 9 /*
10 - (void)addEmployeesObject:(NSManagedObject *)value {    
11     NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
12     [self willChangeValueForKey:@"employees" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
13     [[self primitiveValueForKey:@"employees"] addObject:value];
14     [self didChangeValueForKey:@"employees" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
15     [changedObjects release];
16 }
17 
18 - (void)removeEmployeesObject:(NSManagedObject *)value {
19     NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
20     [self willChangeValueForKey:@"employees" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
21     [[self primitiveValueForKey:@"employees"] removeObject:value];
22     [self didChangeValueForKey:@"employees" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
23     [changedObjects release];
24 }
25 
26 
27 - (void)addEmployees:(NSSet *)value {    
28     [self willChangeValueForKey:@"employees" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
29     [[self primitiveValueForKey:@"employees"] unionSet:value];
30     [self didChangeValueForKey:@"employees" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
31 }
32 
33 - (void)removeEmployees:(NSSet *)value {
34     [self willChangeValueForKey:@"employees" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
35     [[self primitiveValueForKey:@"employees"] minusSet:value];
36     [self didChangeValueForKey:@"employees" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
37 }
38 */
39 - (void)addEmployeesObject:(Employee *)value
40 {
41     NSLog(@"Dept %@ adding employee %@", [self deptName], [value fullName]);
42     NSSet *= [NSSet setWithObject:value];
43     [self willChangeValueForKey:@"employees" withSetMutation:NSKeyValueUnionSetMutation usingObjects:s];
44     [[self primitiveValueForKey:@"employees"] addObject:value];
45     [self didChangeValueForKey:@"employees" withSetMutation:NSKeyValueUnionSetMutation usingObjects:s];
46 }
47 
48 - (void)removeEmployeesObject:(Employee *)value
49 {
50     NSLog(@"Dept %@ removing employee %@", [self deptName], [value fullName]);
51     Employee *manager = [self manager];
52     if (manager == value) 
53     {
54         [self setManager:nil];
55     }
56     NSSet *= [NSSet setWithObject:value];
57     [self willChangeValueForKey:@"employees" withSetMutation:NSKeyValueMinusSetMutation usingObjects:s];
58     [[self primitiveValueForKey:@"employees"] removeObject:value];
59     [self didChangeValueForKey:@"employees" withSetMutation:NSKeyValueMinusSetMutation usingObjects:s];
60 }
61 
62 @end

posted on 2011-04-06 19:59  Ray Z  阅读(230)  评论(0编辑  收藏  举报

导航