Eugene
Eugene

Reputation: 155

Can't write NSDictionary parameters into custom Object

I was stuck on writing NSDictionary into Object process, I am sure that problem is simple as I imagine but would be great to get assistant. Here is my code:

my custom object:

@interface User : NSObject

@property (nonatomic, retain) NSString *cId;
@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *lastName;
....

-(instancetype) initWithParameters:(NSDictionary*) parameters;

@end


#import "User.h"

@implementation User

-(instancetype) initWithParameters:(NSDictionary*) parameters
{
    self = [super init];
    if (self) {
        [self setParameters:parameters];
    }
    return self;
}

- (void) setParameters:(NSDictionary*) parameters{
    _cId = parameters[@"cId"];
    _firstName = parameters[@"first_name"];
    _lastName = parameters[@"last_name"];
    ....
} 

and writing process:

id userObjects = [resultData objectForKey:@"data"];
NSMutableArray* mUsers = [[NSMutableArray alloc] init];
for (NSDictionary* userParameters in userObjects) {
    User *user = [[User alloc] initWithParameters:userParameters];
    [mUsers addObject:user];
}

userObjects - NSArray got from JSON object from server data.

The problem is : nothing happening and user object still empty after initialization, then I have tried - setValuesForKeysWithDictionary after I called variables same as keys in dictionary and nothing changed.

enter image description here

after adding in mUsers: enter image description here

Could anybody tell me what I am doing wrong? Thank you!

Upvotes: 1

Views: 111

Answers (1)

trojanfoe
trojanfoe

Reputation: 122391

I believe you think those objects are uninitialized because you are seeing 0 key/value pairs next to each User object.

Your code looks good and I think things will change once you implement [NSObject description] (or [NSObject debugDescription]) like this:

- (NSString *)description
{
    return [NSString stringWithFormat:@"cId=%@, firstName=%@, lastName=%@",
        _cId, _firstName, _lastName];
}

Upvotes: 1

Related Questions