简体   繁体   中英

core data fetching the information in the array objective-c

I'm quite new to Objective-C. I've been trying to fetch from my core data entity. The following code fetches the right row because it only returns 1 result. but when I try to NSlog() it to see its values what I get is:

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

I am used to PHP that I just do a var_dump() and i get all the information in the array... specially when I am expecting 16 results as this entity has 16 fields.

Could anyone tell me how could I inspect that array? and also, most important, how do I ultimately do this: fetch the Gender field of the matched email field and assign it to a NSString variable.

The query i want to do in sql is 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;
}

This fetching code is happening in my genderPickerViewController : UIViewController

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

returns

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

This is my 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

Is the user object a NSDictionary or an NSArray by chance? If that is the case you are simply logging out the object, you will need to specify a specific entity in the object to output.

For example if it's a NSDictionary

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

You could also try

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

What you are doing is right, you usually get the full objects and not only one attribute, Core Data is an object storage.

So you only need [((User *)matches[0]) gender] to get the gender of the returned user. If you want to get some extra information from objects you must implement:

- (NSString *)description

It is used for converting to string and logging.

You can use the following code,

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

OR

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

then you can print the fetched results as type po OBJECT_NAME.PROPERTY in debug area

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM