2012年3月17日 星期六

神奇的KVC ( key-value coding)

[myObj setValue:@"peter" forKey:@"name"];
(1) 呼叫 setName:
(2) 如果setName:不存在,存取instance variable name。
NSLog(@"name %@", [mutableArray valueForKey:@"name"]);
回傳array,array裡為對mutableArray裡的每個物件呼叫valueForKey:的結果。
ex:
->
name (
Peter,
Wendy
)
Artist *artist = [[Artist alloc] init];
Album *album = [[Album alloc] init];
[artist setValue:album forKey:@"newAlbum"];
[artist setValue:@"怎麼開始忘了" forKeyPath:@"newAlbum.name"];
NSLog(@"album name %@", [artist valueForKeyPath:@"newAlbum.name"]);
利用keyPath,可以串聯
view raw gistfile1.m hosted with ❤ by GitHub

沒有留言:

張貼留言