Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在NSFetchedResultsController中访问子实体关系/属性_Iphone_Core Data - Fatal编程技术网

Iphone 在NSFetchedResultsController中访问子实体关系/属性

Iphone 在NSFetchedResultsController中访问子实体关系/属性,iphone,core-data,Iphone,Core Data,我试图从我的一个实体中的managedobject关系访问属性,但我一直从中获取一个“nil”对象。这里是实体与实体之间关系的总结 实体:房屋 关系:地址(1到1) 实体:地址 物业:街道(NSString) 关系:房屋(1对1) 所以基本上一个“房子”只能有一个“地址”。我想在我买房子时,能够根据街道属性和地址关系进行排序。这是我正在使用的代码,但似乎无法实现。我错过了什么 - (NSFetchedResultsController *)fetchedResultsController {

我试图从我的一个实体中的managedobject关系访问属性,但我一直从中获取一个“nil”对象。这里是实体与实体之间关系的总结

实体:房屋 关系:地址(1到1)

实体:地址 物业:街道(NSString) 关系:房屋(1对1)

所以基本上一个“房子”只能有一个“地址”。我想在我买房子时,能够根据街道属性和地址关系进行排序。这是我正在使用的代码,但似乎无法实现。我错过了什么

- (NSFetchedResultsController *)fetchedResultsController {
    // Set up the fetched results controller if needed.
    if (fetchedResultsController == nil) {

        MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
        NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext]; 

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

        // Edit the sort key as appropriate.
        NSString *sectionKey = @"adresse.rue";
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sectionKey ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

        [fetchRequest setSortDescriptors:sortDescriptors];
        [fetchRequest setFetchBatchSize:20];

        // Edit the section name key path and cache name if appropriate.
        // nil for section name key path means "no sections".
        NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sectionKey cacheName:@"House"];
        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;

        [aFetchedResultsController release];
        [fetchRequest release];
        [sortDescriptor release];
        [sortDescriptors release];
    }

    return fetchedResultsController;
}    

你的代码看起来不错。您的提取请求有效。如果它没有抛出任何异常,则正确指定了密钥路径

我不太清楚你所说的“从中获取
nil
对象”是什么意思。如果您的意思是“获取的结果”控制器不包含任何对象,请确保在尝试访问任何对象之前调用
performFetch:
。执行此操作之前,控制器将为空

当您省略排序描述符和键路径时,此代码是否有效