简体   繁体   中英

core data NSManagedObject generated functions

I'm having a problem understanding how to use my core data generated NSManagedObject .

Category.h:

@class Product;

@interface Category : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * mName;
@property (nonatomic, retain) NSSet* mProduct;
@end

Category.m:

#import "Category.h"
#import "Product.h"

@implementation Category
@dynamic mName;
@dynamic mProduct;


- (void)addMProductObject:(Product *)value {    
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"mProduct" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"mProduct"] addObject:value];
    [self didChangeValueForKey:@"mProduct" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)removeMProductObject:(Product *)value {
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"mProduct" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"mProduct"] removeObject:value];
    [self didChangeValueForKey:@"mProduct" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];
}

- (void)addMProduct:(NSSet *)value {    
    [self willChangeValueForKey:@"mProduct" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"mProduct"] unionSet:value];
    [self didChangeValueForKey:@"mProduct" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}

- (void)removeMProduct:(NSSet *)value {
    [self willChangeValueForKey:@"mProduct" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"mProduct"] minusSet:value];
    [self didChangeValueForKey:@"mProduct" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}


@end

How do I add a Product to a Category ? By default, these functions( - (void)addMProductObject:(Product *)value , etc) aren't visible when i try this:

Product *product = (Product*)[NSEntityDescription insertNewObjectForEntityForName:@"Product" inManagedObjectContext:[self managedObjectContext]];;
...
Category *cat = (Category*)[NSEntityDescription insertNewObjectForEntityForName:@"Category" inManagedObjectContext:[self managedObjectContext]];
...
[cat addMProductObject:product];

warning: 'Category' may not respond to '-addMProductObject:'

Can i just declare these functions in Category.h so they are visible and use them normally?

Before xcode 4(i think), these functions were declared in the .h file and i could use the without warnings. I believe the functions looked a bit different too. I am creating a new core data project and this is new to me.

Yes, just declare them in the .h file. I think there's a way to have Xcode put the declarations there automatically...

Edit: Yup, you can just copy-and-paste the relationships from the model editor to your .h file. Source

First of all, your addMProductObject is not defined in the .h file. Because of that compiler thinks that it the Category class might not respond to it.

Also, note the following:

Are you using this with OS 10.7 SDK? If yes, maybe even if it's LLVM compiler on iOS 5 beta, I didn't check that, the problem could be that: Category is already taken as a name in the SDK. In Lion SDK, the word Category is already defined in the SDK (since there are class categories).

Try changing class name to something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM