iOS key value coding
Asked Answered
N

1

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"];
Notepaper answered 23/4, 2013 at 7:16 Comment(0)
C
6

The keyPath should fit you requirements.

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

Check doc NSKeyValueCoding Protocol and Key-Value Coding Fundamentals

Collective answered 23/4, 2013 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.