我是否正确地将对象添加到核心数据?

时间:2011-04-27 05:16:06

标签: iphone objective-c core-data

我有一个实体“常规”,与“练习”有很多关系,叫做“​​routineExercises”。

我有一个名为addExercise的方法,它应该将一个练习添加到例程的实例中。

这是在代码中实现此功能的正确方法吗?

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;

更新:我在routine.routineExercises =练习中出现错误“ - [Routine setRoutineExercises:]:无法识别的选择器发送到实例0x711bf30 2011-04-27 01:25:37.683卷曲[888:207] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [例程setRoutineExercises:]:无法识别的选择器发送到实例0x711bf30' “

更新

-(void)addExercise
{
   if (managedObjectContext == nil) 
    { 
        managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
        [managedObjectContext retain];
    }

    NSFetchRequest *request = [[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];
}

enter image description here

错误:

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

1 个答案:

答案 0 :(得分:6)

你错过了对Routine *的强制转换。

更改

Routine *routine = [NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];

Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];