开发者

My NSManagedObject has 2 To-Many relationship, but only one can return correct class information

My NSManagedObject says Tag has 2 To-many relationships,says posts and children. And I let Xcode 4 to generate NSManagedObject subclass for it. But I find that only one relationship can return the correct class information.

So the codes are look like following and even these codes were generated by xcode, you can immediately find something wrong,

@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];

}

So you can find that for Post, it is - (void)addPostsObject: (Post *) value but for SubTab, it is - (void)addChildrenObject:(NSManagedObject *) value // value is NOT a SubTag

And I tried to check the element of posts and children to see if they can return the correct class information. And they did NOT.

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**

Even I changed - (void)addChildrenObject:(NSManagedObject *)value to - (void)addChildrenObject:(SubTag *)value, the result is the same.

I really can't figure out why. Can someone cast some light on it ?

Thanks a lot!


I deleted the original SubTag that xcode generated for me and let xcode generate again and the problem was solved.

I don't know why but did not bother to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜