13

我正在尝试从我的核心数据库中获取单个对象,但是它一直返回 null。我的方法基于另一种方法,该方法返回我正在访问的 coredata 对象的每个值。

我以前从未尝试过,也尝试过阅读苹果文档,但它只是没有意义..这就是我的方法的样子

- (NSMutableArray *)readSelectedInstall:(NSString *)projIDString {
    NSManagedObjectContext *context = [self managedObjectContext];

    if (context == nil) {
        NSLog(@"Nil");
    }
    else {


        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"InstallProject" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ProjID==%@", projIDString];
        [fetchRequest setPredicate:predicate];

        NSError *error;

        NSMutableArray *installProjectDictionaryArray = [[NSMutableArray alloc] init];


        NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
        for (InstallProject *installProj in fetchedObjects) {

            NSMutableDictionary *tempInstallProjectDictionaryArray = [[ NSMutableDictionary alloc] init];

            [tempInstallProjectDictionaryArray setObject:installProj.companyName forKey:@"CompanyName"];
            [tempInstallProjectDictionaryArray setObject:installProj.projNo forKey:@"ProjNo"];
            [tempInstallProjectDictionaryArray setObject:installProj.projID forKey:@"ProjID"];

            [installProjectDictionaryArray addObject:tempInstallProjectDictionaryArray];
        }

        return installProjectDictionaryArray;
    }
    return nil;
}

任何帮助我返回 projID 与 projIDString 匹配的单个项目将不胜感激。

4

2 回答 2

18

导入 NSManagedObject(InstallProject) 并像这样获取一个对象,

-(InstallProject *)readSelectedInstall:(NSString *)projIDString
{

    NSArray *fetchedObjects;
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"InstallProject"  inManagedObjectContext: context];
    [fetch setEntity:entityDescription];
    [fetch setPredicate:[NSPredicate predicateWithFormat:@"(ANY ProjID contains[cd] %@)",projIDString]];
    NSError * error = nil;
    fetchedObjects = [context executeFetchRequest:fetch error:&error];

    if([fetchedObjects count] == 1)
    return [fetchedObjects objectAtIndex:0];
    else
    return nil;  

}
于 2013-10-16T07:16:54.067 回答
16

你可以将它设置fetchLimit1fetchRequest

获取限制指定请求在执行时应返回的最大对象数。

例如在 Swift 中:

let fetchRequest = NSFetchRequest(entityName: "Cart")
fetchRequest.fetchLimit = 1
fetchRequest.sortDescriptors = [NSSortDescriptor(key: #keyPath(Cart.creationDate), ascending: true)]

编辑:设置这些sortDescriptors只是因为我将它fetchRequest与 NFRC 一起使用,你也可以执行fetchRequest

do {
  let fetchRequest = NSFetchRequest(entityName: "Cart")
  fetchRequest.fetchLimit = 1
  fetchRequest.predicate = NSPredicate(format: "name == %@", mainCart)
  var objects: [Cart]
  try objects = mainMoc.execute(fetchRequest) as! [Cart]
} catch {
  
}
于 2015-12-14T18:15:22.737 回答