NSPredicate: Fetch one of each kind

后端 未结 5 1892
眼角桃花
眼角桃花 2021-02-02 11:49

I want to create an NSFetchRequest for objects like this:

The Object is Car which has an attribute color

5条回答
  •  旧巷少年郎
    2021-02-02 12:28

    try this:

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity =
    [NSEntityDescription entityForName:@"Selected_data"
                inManagedObjectContext:self.managedObjectContext];
    [request setEntity:entity];
    NSPredicate *predicatecnName = [NSPredicate predicateWithFormat:@"countries contains[cd] %@", Countryid];
    NSPredicate *predicateLn = [NSPredicate predicateWithFormat:@"languages contains[cd] %@", languageid];
    NSArray *subPredicates = [NSArray arrayWithObjects:predicatecnName, predicateLn, nil];
    
    NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates];
    
    
    [request setPredicate:predicate];
    NSMutableArray *sendfd=[[NSMutableArray alloc] init];
    NSError *error;
    NSArray *array1 = [self.managedObjectContext executeFetchRequest:request error:&error];
    
    
    if (array1 != nil) {
    
    
        for (Selected_data *sld in array1)
        {
    
            [sendfd addObject:sld.providers];
        }
    
    
    }
    else {
    
    }
    return sendfd;
    

提交回复
热议问题