我的NSManagedObject说Tag有2个To-many关系,帖子和孩子说。我让Xcode 4为它生成NSManagedObject子类。但我发现只有一个关系可以返回正确的类信息。
所以代码看起来如下,即使这些代码都是由xcode生成的,你也可以立即发现错误,
@interface Tag : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet* children;
@property (nonatomic, retain) NSSet* posts;
@end
...
- (void)addChildrenObject:(NSManagedObject *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"children" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"children"] addObject:value];
[self didChangeValueForKey:@"children" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
- (void)addPostsObject:(Post *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"posts" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"posts"] addObject:value];
[self didChangeValueForKey:@"posts" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
所以你可以找到对于Post,它是 - (void)addPostsObject:(Post *)值 但对于SubTab,它是 - (void)addChildrenObject:(NSManagedObject *)value // value不是SubTag
我试图检查帖子和子元素,看看他们是否可以返回正确的类信息。他们没有。
Tag* data = ...
NSObject *a = [tmp.posts anyObject];
NSString *className = NSStringFromClass([a class]);
NSLog(@"I am a %@",className); //it says Post correctly.
Tag* data = ...
NSObject *a = [tmp.children anyObject];
NSString *className = NSStringFromClass([a class]);
NSLog(@"I am a %@",className); //it says **NSManagedObject** instead of **SubTag**
即使我改变了 - (void)addChildrenObject :( NSManagedObject *)值为 - (void)addChildrenObject:(SubTag *)值,结果是一样的。
我真的无法弄清楚为什么。有人能对它施加一些启示吗?
非常感谢!
答案 0 :(得分:0)
我删除了为我生成的xcode的原始SubTag,让xcode再次生成,问题解决了。
我不知道为什么但又懒得去。