2011-04-24 82 views
0

我在iphone中使用核心數據的數據庫。我想只搜索一列信息。 我的示例代碼是;iphone sdk - 核心數據:關於謂詞,實體等

#import <Foundation/Foundation.h> 
#import <CoreData/CoreData.h> 


@interface Groups : NSManagedObject { 
    @private 
} 
@property (nonatomic, retain) NSString * GroupID; 
@property (nonatomic, retain) NSString * userID; 
@property (nonatomic, retain) NSString * memberID; 
@property (nonatomic, retain) NSString * memberNAME; 
@property (nonatomic, retain) NSString * memberLNAME; 
@property (nonatomic, retain) NSString * memberNUMBER; 
@property (nonatomic, retain) NSString * memberBIRTH; 
@property (nonatomic, retain) NSString * memberMARRIAGE; 

@end 


-(void)GetDataFromDB{ 


     app = (CepostaAppDelegate*)[[UIApplication sharedApplication] delegate]; 

     root = (RootViewController*) 
        ([app.navigationController.viewControllers objectAtIndex:0]); 

     NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

     NSEntityDescription *entity = [NSEntityDescription 
        entityForName:@"Groups" 
        inManagedObjectContext:app.managedObjectContext]; 


     NSPredicate *predicate = [NSPredicate 
        predicateWithFormat:@"(GroupID IN %@)",SendGrupList]; 

     [request setEntity:entity]; 
     [request setPredicate:predicate]; 

     NSError *error; 
     NSMutableArray *TempArr = [[NSMutableArray alloc] initWithArray: 
      [app.managedObjectContext executeFetchRequest:request error:&error]]; 

     [request release]; 

     for (gObje in TempArr) { //gObje is Groups's Object 
      NSString *GroupsMemNum = [[NSString alloc] 
            initWithString:gObje.memberNUMBER]; 
      [gGonder.TeLList addObject:GroupsMemNum]; 
      NSLog(@"%@",GroupsMemNum); 
      [GroupsMemNum release]; 
     } 
} 

我做出SendGrupList與謂詞搜索(它有組ID的),但TempArr返回數據庫中的所有數據,我怎麼能得到的只是一點的信息?

回答

0

,你將不得不使用refrences像

(refGroupTableName.groupId IN %@"),SendGrupList 
+0

它不工作:S – relower 2011-04-24 15:22:13

0

你是什麼SendGrupList?如果它是一個數組,它應該可以工作。 這裏是一個工作示例,其中SendGrupListNSMutableArray

[SendGrupList addObject:data]; 
    NSError *error = nil; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSPredicate *predicate = [NSPredicate 
         predicateWithFormat:@"GroupID IN %@",SendGrupList]; 

    [fetchRequest setPredicate:predicate]; 

    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"Groups" 
           inManagedObjectContext:managedObjectContext]; 
    [fetchRequest setEntity:entity]; 
    NSArray *fetchedObjects = [managedObjectContext 
           executeFetchRequest:fetchRequest error:&error];