iphone sdk - 核心数据:关于谓词、实体等

发布于 2024-11-02 23:54:17 字数 1926 浏览 0 评论 0原文

我在 iPhone 中使用带有核心数据的数据库。我只想通过搜索获得一列信息.. 我的示例代码是;

#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 返回数据库中的所有数据,如何才能只获取一列信息?

i use a database with core data in iphone. i wanna get just one column information with a search..
My sample code is;

#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];
        }  
}

I make a search with predicate in SendGrupList (it has group id's), but TempArr returns all data in db, how can I get just one column information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑕疵 2024-11-09 23:54:17

你将不得不使用类似的参考文献

(refGroupTableName.groupId IN %@"),SendGrupList

you will have to use refrences like

(refGroupTableName.groupId IN %@"),SendGrupList
烟花肆意 2024-11-09 23:54:17

您的 SendGrupList 是什么?如果它是一个数组,它应该可以工作。
这是一个工作示例,其中 SendGrupList 是一个 NSMutableArray

[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];

What is your SendGrupList? If it's an array, it should work.
Here is a working example, where SendGrupList is a NSMutableArray

[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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文