Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create NSObject from NSDictionary in objective-c

I would like to assign each item from a dictionary into my object using a "for...in" loop

I have a NSDictionary like that :

{"user_id" : "1", "user_name" : "John"}

And I have an NSObject with variable :

NSString *user_id;
NSString *user_name

With a for in loop, I want to assign each item from my NSDictionary in my NSObject

for (NSString *param in userDictionary) {

}

How I can do that ?

Thanks,

like image 290
alexmngn Avatar asked Dec 06 '22 16:12

alexmngn


1 Answers

Have a look at the NSObject class method:

- (void)setValue:(id)value forKey:(NSString *)key;

For example:

[userDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
    [myObject setValue:obj forKey:(NSString *)key];
}];
like image 60
sch Avatar answered Jan 02 '23 00:01

sch