iand675
iand675

Reputation: 1118

Is there a way to construct an object automatically from an NSDictionary?

I'm building an iPhone app that communicates with an external server via JSON. The JSON library I'm using parses the response string from the server into a dictionary. Currently I've got a method that I've written that just uses hardcoded strings as keys for the dictionary in a constructor I've written called initWithDictionary:(NSDictionary *)dic (e.g. self.name = [dic valueForKey:@"name"];. Is there some smart idiomatic Objective-C way to do this?

Upvotes: 1

Views: 76

Answers (1)

Chuck
Chuck

Reputation: 237060

Just call [someObject setValuesForKeysWithDictionary:dictFromJSON] to set an object's properties from a dictionary.

Upvotes: 5

Related Questions