我对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