为什么我会收到此 NSManaged 对象错误

发布于 2024-11-03 17:46:18 字数 2964 浏览 0 评论 0原文

[theSelectedRoutine addExerciseObject:exercise]; 行出现错误,提示未找到方法 -addExerciseObject。

界面: @property(非原子,保留)例程*theSelectedRoutine;

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

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(exerciseChooser)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];

    NSError *error = nil;

    Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];

    exercise.name = selectedExercise;

    [theSelectedRoutine addExerciseObject: exercise];

    if (![managedObjectContext save:&error]) 
    {
        // Handle the error.
    }
    NSLog(@"%@", error);

    [self.routineTableView reloadData];

}

..

Routine Class
#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

Getting error at line [theSelectedRoutine addExerciseObject: exercise]; saying method -addExerciseObject not found.

Interface:
@property (nonatomic, retain) Routine *theSelectedRoutine;

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

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(exerciseChooser)];
    self.navigationItem.rightBarButtonItem = addButton;
    [addButton release];

    NSError *error = nil;

    Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];

    exercise.name = selectedExercise;

    [theSelectedRoutine addExerciseObject: exercise];

    if (![managedObjectContext save:&error]) 
    {
        // Handle the error.
    }
    NSLog(@"%@", error);

    [self.routineTableView reloadData];

}

..

Routine Class
#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘曦 2024-11-10 17:46:18

好吧,我也没有看到 addExerciseObject ,它不应该是 [theSelectedRoutine addRoutineExercisesObject:exercise]; 吗?

Well I don't see addExerciseObject either, shouldn't it be [theSelectedRoutine addRoutineExercisesObject:exercise]; ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文