iPhone:类似于;其中;核心数据中的type子句

iPhone:类似于;其中;核心数据中的type子句,iphone,objective-c,core-data,Iphone,Objective C,Core Data,我第一次在我的应用程序中使用核心数据来解决我的问题。我在谷歌上做了很多工作,但我没有找到具体的解决方案 我有实体:子类别 属性:主类别ID,子类别名称 现在我想像这样获取数据 Select subCategoryName from SubCategory where mainCategoryId=1; 我实施的代码如下: NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *enti

我第一次在我的应用程序中使用核心数据来解决我的问题。我在谷歌上做了很多工作,但我没有找到具体的解决方案

我有实体:子类别

属性:主类别ID,子类别名称

现在我想像这样获取数据

Select subCategoryName from SubCategory where mainCategoryId=1;
我实施的代码如下:

 NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SubCategory" inManagedObjectContext:managedObjectContext];

    [request setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SubCategoryName == %@",@"Triangular1"];
    [request setPredicate:predicate];

    NSError *error;
    arrSubCategory = [managedObjectContext executeFetchRequest:request error:&error];
如何在coredata中获取类似类型的数据

分享你的想法


从你的问题来看,你需要的是

[NSPredicate predicateWithFormat:@"mainCategoryId = %@",@"1"];
所以你的代码看起来像

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

NSPredicate *predicate =[NSPredicate predicateWithFormat:@"mainCategoryId = %@",@"1"];
[request setPredicate:predicate];

NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
    NSLog(@"subCategoryName: %@", [info valueForKey:@"subCategoryName"]);
}
以上只是一个示例代码。根据您的需求,实际实现可能会有所不同


这是一本关于。你也可以这样做。

你可以这样做

NSString *maincategoryId = @"1";
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"SubCategory"];
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"rubriqueid == %@", maincategoryId]];
在调用NSMutableArray以放入结果之后:

NSMutableArray *answensmuarray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

你能发布你迄今为止尝试过的代码吗?