Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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
Ios 从UITableView获取核心数据对象,而不是从核心数据生成_Ios_Objective C_Core Data_Nsfetchedresultscontroller - Fatal编程技术网

Ios 从UITableView获取核心数据对象,而不是从核心数据生成

Ios 从UITableView获取核心数据对象,而不是从核心数据生成,ios,objective-c,core-data,nsfetchedresultscontroller,Ios,Objective C,Core Data,Nsfetchedresultscontroller,我试图获取一个核心数据对象,并从一个不是由核心数据对象组成的UITableView加载它。它只是一个用预先确定的数组创建的UITableView,但我想在核心数据中加载一个与单元格同名的对象detailTextLabel。我试图加载一个特定的值,但它不起作用:它崩溃了,或者告诉我我的实体是nil,但事实并非如此 因此,基本上我有一个UITableView,它与这个数组一起加载: array = @[@"publication 1",@"publication 2", etc] 然后,当用户选择

我试图获取一个核心数据对象,并从一个不是由核心数据对象组成的
UITableView
加载它。它只是一个用预先确定的数组创建的
UITableView
,但我想在核心数据中加载一个与单元格同名的对象
detailTextLabel
。我试图加载一个特定的值,但它不起作用:它崩溃了,或者告诉我我的实体是
nil
,但事实并非如此

因此,基本上我有一个
UITableView
,它与这个数组一起加载:

array = @[@"publication 1",@"publication 2", etc]
然后,当用户选择单元格时,我希望它基于单元格
detailText
加载文档,但它总是加载对象
indexPath
是什么,而不是基于单元格
detailText
。比如说,在主视图控制器中,预先确定的数组被加载到表视图中,我点击Memo 24,但是它的索引是(0,2)。它将加载一个对象,但它将加载下载实体中的对象,该实体具有与Memo 24标题相反的相同的
indexPath
(无论发布的编号是0,2)

这是我的密码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    [self loadPublicationLocallyWithPubAtIndex:indexPath withTitle:cell.detailTextLabel.text];
}

- (void)loadPublicationLocallyWithPubAtIndex:(NSIndexPath *)indexPath withTitle:(NSString *)pubNumber {
    NSManagedObjectContext *selectedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
    NSString *filePath;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"PDFs"];
    filePath = [filePath stringByAppendingPathComponent:[selectedObject valueForKey:@"puburl"]]; assert(filePath != nil); ;
    NSLog(@"Local path is : %@", filePath);
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        //File exists
        NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
        if (data)
        { 
            //load document
        }
    }
}
它适用于所有填充了核心数据的视图控制器,无论单元格是什么,它仍然检索正确的对象,因此我相信它与
[self.fetchedResultsController objectAtIndex:]有关部分,所以我甚至尝试:

NSIndexPath *selectedItem = [self.fetchedResultsController indexPathForObject:cell.detailTextLabel.text];
中选择了rowatinexpath:
,但它也不起作用


如何基于单元格
detailTextLabel
而不是其
indexPath
加载对象?

如果从静态数组填充数组,则可能不需要使用获取结果控制器。您应该只执行一个fetch来获取相关的NSManagedObject。要确保fetch只获取相关对象,请使用谓词

假设NSManagedObjects位于
出版物
实体中,且相关属性为
标题
。您应该像这样构建一个fetch:

NSManagedObjectContext *context = self.managedObjectContext; // or however your context is obtained
NSManagedObject *selectedObject;
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Publication"]; // amend entity name as appropriate
fetch.predicate = [NSPredicate predicateWithFormat:@"title == %@",pubNumber];
NSError *error;
NSArray *results = [context executeFetchRequest:fetch error:&error];
if (results == nil) {
    NSLog(@"Fetch error %@, %@", error, [error userInfo]);
    abort();
} else {
    if ([results count] == 0) {
        NSLog(@"Publication not found");
    } else {
        // (note, should probably also check for count > 1 and handle accordingly)
        selectedObject = results[0];
        // then carry on with the remaining code from loadPublicationLocally....
        NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
        NSString *filePath;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"PDFs"];
        filePath = [filePath stringByAppendingPathComponent:[selectedObject valueForKey:@"puburl"]]; assert(filePath != nil); ;
        // ... etc etc.
    }
}

非常感谢。我已经尝试使用NSPredicate三天了。你是一个救命恩人,我是一个新的编码(显然)和我的大部分代码是通过这样获得的,所以希望这个答案将有助于未来的问题寻求者!谢谢,谢谢你的评论和解释