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