I have an entity "Routine" with a to many relationship to "Exercise" called "routineExercises".
I have a method called addExercise
which should add an Exercise to an instance of Routine.
Is this the correct way to implement this in code?
Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
exercise.name = selectedExercise;
NSMutableSet *exercises = [[NSMutableSet alloc]init];
[exercises addObject:exercise];
Routine *routine = [NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];
routine.routineExercises = exercises;
Update: I am getting an error at routine.routineExercises = exercises that says "-[Routine setRoutineExercises:]: unrecognized selector sent to instance 0x711bf30 2011-04-27 01:25:37.683 Curl[888:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Routine setRoutineExercises:]: unrecognized selector sent to instance 0x711bf30' "
Update:
-(void)addExercise
{
if (managedObjectContext == nil)
{
managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
[managedObjectContext retain];
}
NSFetchRequest *reques开发者_StackOverflowt = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
// Handle the error.
}
Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
exercise.name = selectedExercise;
NSMutableSet *exercises = [NSSet setWithObjects:exercise,nil];
Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];
routine.routineExercises = exercises;
if (![managedObjectContext save:&error])
{
// Handle the error.
}
NSLog(@"%@", error);
[self.routineTableView reloadData];
[mutableFetchResults release];
[request release];
}
Error:
2011-04-27 03:05:58.902 Curl[1954:207] -[Routine setRoutineExercises:]: unrecognized selector sent to instance 0x5d4a4e0
2011-04-27 03:05:58.904 Curl[1954:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Routine setRoutineExercises:]: unrecognized selector sent to instance 0x5d4a4e0'
Routine.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Exercise;
@interface Routine : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * timeStamp;
@property (nonatomic, retain) NSSet* routineExercises;
@end
Routine.m
#import "Routine.h"
#import "Exercise.h"
@implementation Routine
@dynamic name;
@dynamic timeStamp;
@dynamic routineExercises;
- (void)addRoutineExercisesObject:(Exercise *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"routineExercises"] addObject:value];
[self didChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[changedObjects release];
}
- (void)removeRoutineExercisesObject:(Exercise *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"routineExercises"] removeObject:value];
[self didChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[changedObjects release];
}
- (void)addRoutineExercises:(NSSet *)value {
[self willChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
[[self primitiveValueForKey:@"routineExercises"] unionSet:value];
[self didChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}
- (void)removeRoutineExercises:(NSSet *)value {
[self willChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
[[self primitiveValueForKey:@"routineExercises"] minusSet:value];
[self didChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}
@end
You are missing the cast to Routine *.
Change
Routine *routine = [NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];
to
Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];
精彩评论