7

Let's assume we have an object of type person, which has a property called name.

Person *p;
[p setValue:@"John" forKey:@"name"];

This works very nicely. What I want to do is dig deeper. Say the class person has another property called address which is of class Address and has a field called zipcode.

Is there a simpler way of assigning the zipcode from the person than this? Maybe something cleaner and clearer?

[[p valueForKey:@"address"] setValue:@"234567" forKey:@"zipcode"];
Teo
  • 3,394
  • 11
  • 43
  • 73

1 Answers1

6

The keyPath should fit you requirements.

[p  setValue:@"234567" forKeyPath:@"address.zipcode"];

Check doc NSKeyValueCoding Protocol and Key-Value Coding Fundamentals

Anupdas
  • 10,211
  • 2
  • 35
  • 60
Andrea
  • 26,120
  • 10
  • 85
  • 131