添加对象到什么类型的变量?

发布于 2024-11-03 19:49:44 字数 569 浏览 1 评论 0原文

我将使用这种方法将锻炼实体添加到例程中。我如何声明 SelectedRoutine 以及它是什么类型?我想我将其设置在此表的父表的 didSelectRow 方法中。

-(void)addExercise
{   
    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];
}

I will be using this method to add an entity of Exercise to a Routine. How can I declare theSelectedRoutine and what type is it? I'm thinking I set it in this table's parent table's didSelectRow method.

-(void)addExercise
{   
    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];
}

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

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

发布评论

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

评论(1

九局 2024-11-10 19:49:45

有几种解决方案。首先,如果您不想为托管对象使用子类,您可以获取关系的可变集并向其中添加新的练习。

-(void)addExercise
{   
  NSError *error = nil;
  Exercise *exercise = [NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
  [exercise setName:selectedExercise];
  [[theSelectedRoutine mutableSetForKey:@"exercise"] addObject:exercise];

  if (![managedObjectContext save:&error]) {
    NSLog(@"Failed to save: %@\n%@", [error localizedDescription], [error userInfo]);
  }

  [[self routineTableView] reloadData];
}

另一个目标是为您的例程创建一个子类:

@interface Routine : NSManagedObject

- (void)addExerciseObject:(NSManagedObject*)exercise;

@end

@implementation Routine

- (void)addExerciseObject:(Employee *)value
{
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    NSMutableSet *primitiveEmployees = [self primitiveValueForKey:@"exercise"];

    [self willChangeValueForKey:@"exercise" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveEmployees] addObject:value];
    [self didChangeValueForKey:@"exercise" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];

    [changedObjects release];
}

@end

然后您可以从那里直接调用 -addExerciseObject:

-(void)addExercise
{   
  NSError *error = nil;
  Exercise *exercise = [NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
  [exercise setName:selectedExercise];
  [theSelectedRoutine addExerciseObject:exercise];

  if (![managedObjectContext save:&error]) {
    NSLog(@"Failed to save: %@\n%@", [error localizedDescription], [error userInfo]);
  }

  [[self routineTableView] reloadData];
}

请注意,无论哪种情况,您不需要都需要从 -insertNewObjectForEntityForName: inManagedObjectContext: 中设置锻炼对象的大小写。该调用的返回结果是 id,并且 id 永远不需要进行强制转换。一般来说,Objective-C 中的类型转换是错误的。

There are a couple of solutions. First, if you don't want to use subclasses for your managed object you can grab the mutable set for the relationship and add your new exercise to it.

-(void)addExercise
{   
  NSError *error = nil;
  Exercise *exercise = [NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
  [exercise setName:selectedExercise];
  [[theSelectedRoutine mutableSetForKey:@"exercise"] addObject:exercise];

  if (![managedObjectContext save:&error]) {
    NSLog(@"Failed to save: %@\n%@", [error localizedDescription], [error userInfo]);
  }

  [[self routineTableView] reloadData];
}

The other object would be to create a subclass for your routine:

@interface Routine : NSManagedObject

- (void)addExerciseObject:(NSManagedObject*)exercise;

@end

@implementation Routine

- (void)addExerciseObject:(Employee *)value
{
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    NSMutableSet *primitiveEmployees = [self primitiveValueForKey:@"exercise"];

    [self willChangeValueForKey:@"exercise" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveEmployees] addObject:value];
    [self didChangeValueForKey:@"exercise" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];

    [changedObjects release];
}

@end

From there you can then call -addExerciseObject: directly.

-(void)addExercise
{   
  NSError *error = nil;
  Exercise *exercise = [NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
  [exercise setName:selectedExercise];
  [theSelectedRoutine addExerciseObject:exercise];

  if (![managedObjectContext save:&error]) {
    NSLog(@"Failed to save: %@\n%@", [error localizedDescription], [error userInfo]);
  }

  [[self routineTableView] reloadData];
}

Note that in either case you DO NOT need to case your Exercise object from -insertNewObjectForEntityForName: inManagedObjectContext:. The return for that call is an id and id never needs to be cast. In general, casting in Objective-C is wrong.

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