【问题标题】:How to check whether an Entity is related or not to another entity如何检查一个实体是否与另一个实体相关
【发布时间】:2013-05-10 15:54:39
【问题描述】:

我是 Core Data 的新手

我有 2 个实体 RoutineExercise,它们与 Routine.exercise 和 Exercise.routine 相关。 用户例程有名称,用户练习也有名称。

我试过了 并且还尝试了 jsut Routine.exercise = Exercise 但这给了我一个警告说 Comparision of distinct pointer type (NSSET and Exercise) 我也尝试做 Routine.exercise.name = @"theName" 但我正在推动它,因为我无法获得这样的运动名称......

if (Routine.exercise == Exercise.routine) {
NSLOG(@"YES");
} else {
NSLOG(@"NO");
}

基本上我只想知道运动是否与常规建立了关系。有人可以帮我解决这个问题吗?

是多对多关系

【问题讨论】:

  • A.userB 是一对多关系吗?如果是这样,那将解释警告,因为它们存储为 NSSet。
  • 是多对多关系

标签: ios xcode cocoa-touch core-data


【解决方案1】:

试试这样的:

获取要测试的对象

Routine *testRoutine = ...;
Exercise *testExercise = ...;

检查练习是否包含在关系中的对象集中

if ([testRoutine.exercise containsObject:testExercise]) {
    ...
}

【讨论】:

    【解决方案2】:

    由于这是一个多对多的关系,你不能使用Routine.exercise = Exercise,因为这个关系是一个 NSMutableSet。以下是将对象添加到关系中的正确方法:

    NSMutableSet *rel = [routineObject mutableSetValueForKey:@"exercise"];
    [rel addObject:exerciseObject];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 2020-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-15
      相关资源
      最近更新 更多