核心数据获取数组objective-c中的信息

时间:2013-04-16 15:47:42

标签: iphone ios objective-c core-data

我对Objective-C很新。我一直在尝试从我的核心数据实体中获取。以下代码获取右行,因为它只返回1个结果。 但当我尝试NSlog()时,我会看到它的价值所在:

iGym[3922:c07] (
    "<User: 0x8397690> (entity: User; id: 0x8341580 <x-coredata://114815EF-85F4-411F-925B-8479E1A94770/User/p19> ; data: <fault>)"
)

我已经习惯了PHP,我只是做了var_dump()而且我得到了数组中的所有信息......特别是当我期待16个结果时,因为这个实体有16个字段。

有人能告诉我怎么检查那个阵列? 最重要的是,我如何最终执行此操作:获取匹配的Gender字段的email字段,并将其分配给NSString变量。

我想在sql中执行的查询是SELECT Gender FROM myTable WHERE email = "something@something.com;

-(NSInteger*)selectedGenderMethod
{
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    request.predicate = [NSPredicate predicateWithFormat:@"email = %@",_currentUser];
    NSError *error = nil;
    NSArray *matches = [[context executeFetchRequest:request error:&error] mutableCopy];
    NSString * someVar= [matches[0] objectForKey:@"email"];
    NSLog(@"%@",someVar);
//More to come once this is sorted
    return 0;
}

此提取代码发生在我的genderPickerViewController : UIViewController

NSLog(@"%@", matches[0]);

返回

<User: 0x83a6b00> (entity: User; id: 0x83995f0 <x-coredata://114815EF-85F4-411F-925B-8479E1A94770/User/p19> ; data: <fault>)

这是我的User.h

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


@interface User : NSManagedObject

@property (nonatomic, retain) NSDate * dob;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSNumber * firstTime;
@property (nonatomic, retain) NSString * gender;
@property (nonatomic, retain) NSString * height;
@property (nonatomic, retain) NSNumber * idFB;
@property (nonatomic, retain) NSNumber * idUserExternal;
@property (nonatomic, retain) NSNumber * idUserInternal;
@property (nonatomic, retain) NSNumber * isPT;
@property (nonatomic, retain) NSString * language;
@property (nonatomic, retain) NSString * metricSystem;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * nickname;
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) NSString * surname;
@property (nonatomic, retain) NSString * weight;

@end

User.m

@implementation User

@dynamic dob;
@dynamic email;
@dynamic firstTime;
@dynamic gender;
@dynamic height;
@dynamic idFB;
@dynamic idUserExternal;
@dynamic idUserInternal;
@dynamic isPT;
@dynamic language;
@dynamic metricSystem;
@dynamic name;
@dynamic nickname;
@dynamic password;
@dynamic surname;
@dynamic weight;

@end

3 个答案:

答案 0 :(得分:0)

用户是偶然对象NSDictionary还是NSArray?如果您只是注销对象,则需要在对象中指定要输出的特定实体。

例如,如果它是NSDictionary

NSString *name = [matches[0] objectForKey:@"name"];
NSLog("Name %@", name);

您也可以尝试

NSString *email = (User *)matches[0].email;
NSLog("Email %@", email);

答案 1 :(得分:0)

你正在做的是对的,你通常得到完整的对象而不仅仅是一个属性,Core Data是一个对象存储。

因此,您只需要[((User *)matches[0]) gender]来获取返回用户的性别。 如果您想从必须实现的对象中获取一些额外信息:

- (NSString *)description

用于转换为字符串和日志记录。

答案 2 :(得分:0)

您可以使用以下代码

[NSPredicate predicateWithFormat:@"email CONTAINS[c] %@", _currentUser]; 

OR

[NSPredicate predicateWithFormat:@"email CONTAINS[cd] %@", _currentUser];

然后您可以在调试区域

中将获取的结果打印为类型po OBJECT_NAME.PROPERTY