Iphone Can';我不知道是取回还是关系问题

Iphone Can';我不知道是取回还是关系问题,iphone,objective-c,core-data,Iphone,Objective C,Core Data,我正在将对象练习添加到对象会话(作为关系) 在视图中,我希望获取并显示特定会话对象的练习 现在,它正在显示数据库中的所有练习,而不仅仅是该会话对象的练习 这两个对象之间的关系称为“练习” 如果有人能帮我的话,这是我当前用于获取的代码 // Create the fetch request for the entity. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name

我正在将对象练习添加到对象会话(作为关系)

在视图中,我希望获取并显示特定会话对象的练习

现在,它正在显示数据库中的所有练习,而不仅仅是该会话对象的练习

这两个对象之间的关系称为“练习”

如果有人能帮我的话,这是我当前用于获取的代码

    // Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Exercise" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

LogResultsViewController *logResultsTableViewController = [[LogResultsViewController alloc]init];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat: @"exercises = %@", logResultsTableViewController.selectedSession]];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
更新代码:

- (void) viewDidLoad
{
NSSet *exercises = [self.selectedSession valueForKey:@"exercises"];
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"timeStamp" ascending:YES]];
NSArray *sorted = [exercises sortedArrayUsingDescriptors:sortDescriptors];
sorted = self.exerciseArray;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [exerciseArray count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [exerciseArray objectAtIndex:indexPath.row];
    return cell;
}
当我记录所选会话时,会显示以下内容:

selectedSession:(实体:会话;id:0x7334f70;数据:{
演习‘’;
timeStamp=“2011-05-3104:41:07+0000”;

此外,当我记录名为“练习”的NSSset时,我得到: ()的关系错误、名称练习、isOptional 1、isTransient 0、实体会话、重命名标识符练习、验证谓词( ),警告( ),versionHashModifier(null),目标实体练习,反向关联练习,0x7140790上的最小计数0,最大计数0

更新:

好的,我把CellForRowatinex中的代码改成
Exercise*Exercise=(Exercise*)[exerciseArray objectAtIndex:indexPath.row];
cell.textlab.text=exercise.name;


现在,它显示了练习名称,但它显示的是所有会话的相同练习列表,而不仅仅是它所属的会话。

如果您的
会话的实例与
练习的实例有关系,则无需执行另一次获取来获取会话的练习。您只需按照e关系并直接获取它们——它们将被自动加载。从您的代码中,它看起来像
logResultsTableViewController。selectedSession
Session
的一个实例。如果是这种情况,您可以按如下方式获取该会话的所有练习(假设该关系被称为
练习
):


然后,您可以根据需要对该NSSet进行排序。

谢谢Tom,我想这正是我所需要的。如果我这样做,我是否应该从此NSSet创建一个NSArray,类似于
[[NSMUTABLEARRY ALOC]initWithArray:[inSet allObjects]]
?此外,如果我这样做,我可以删除此视图中所有与fetchresults相关的代码吗?如果我想从此视图中删除练习的实例,该怎么办?嗯,
[插入AllObject]
已经是一个数组,因此不需要其他数组,除非您需要数组是可变的。查看上面的代码,您可以执行类似于
NSArray*sorted=[插入sortedarray使用描述符:sortDescriptors]
。好的,很酷,我现在就要试试。我可以从此viewController中删除所有与fetchresults相关的代码,对吗?但是如果我想删除练习的实例,该怎么办?我仍然可以这样做吗?如果你可以看的话,我还将我的新代码添加到问题中。不过,表中没有显示任何数据。我不确定是否有什么问题使用此代码,或者如果练习的实例未正确添加到会话中。要回答第一条注释,如果要删除条目,则需要转换为可变数组。对于第二个问题,表视图回调使用
exerciseArray
,但没有迹象表明您分配过任何dat此变量的赋值。因此,表中没有要显示的数据。看起来
viewDidLoad
中的最后一行赋值是向后的。
NSSet *exercises = [logResultsTableViewController.selectedSession valueForKey:@"exercises"];