1

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

3 回答 3

0

您所做的是对的,您通常会获得完整的对象而不仅仅是一个属性,Core Data 是一个对象存储。

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

- (NSString *)description

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

于 2013-04-16T15:56:19.307 回答
0

您可以使用以下代码,

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

或者

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

然后您可以将获取的结果打印为po OBJECT_NAME.PROPERTY调试区域中的类型

于 2013-04-16T15:59:24.097 回答
0

用户对象是 aNSDictionary还是NSArray偶然?如果是这种情况,您只是简单地注销对象,则需要在对象中指定要输出的特定实体。

例如,如果它是一个NSDictionary

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

你也可以试试

NSString *email = (User *)matches[0].email;
NSLog("Email %@", email);
于 2013-04-16T15:53:51.120 回答