2012-11-08 4 views
1

내 문제에 대한 응용 프로그램에서 핵심 데이터를 처음 사용하고 있지만 Google을 많이 수행했지만 특정 솔루션을 찾지 못했습니다. 하위 카테고리iPhone : 핵심 데이터의 "where"type 절과 비슷합니다.

속성 : mainCategoryId, subCategoryName

나는 엔티티 있습니다.

는 지금은 같은 데이터를 가져 오기 할

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 데이터의 유사한 종류를 가져올 수

내 아이디어를 공유하십시오.

thanx. 당신이 필요한 질문에서

+0

당신은 당신이 지금까지 시도 코드를 게시 할 수 있습니까? – iDev

답변

1

,

[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"]); 
} 

위는 샘플 코드입니다. 실제 구현은 요구 사항에 따라 다를 수 있습니다.

coredata is here에 대한 자습서. 그것도 확인하십시오.

0

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]; 
관련 문제