iOS KVC valueForKeyPath学习心得

时间:2024-01-19 10:38:32
#import <Foundation/Foundation.h>

@interface myClass1 : NSObject
@property (nonatomic, copy) NSString *str; @end @implementation myClass1 @end @interface myClass : NSObject
{
}
@property (nonatomic, retain) myClass1 *item; @end @implementation myClass - (id)init{
if(self = [super init]){
[self setValue:[[myClass1 alloc]init] forKey:@"item"];//如果不给item设置值,则最后得到的item.str为空!
}
return self;
}
@end int main(int argc, const char * argv[]) {
@autoreleasepool {
[class1 setValue:@"this is item.str" forKeyPath:@"item.str"];
NSLog(@"item.str = %@",[class1 valueForKeyPath:@"item.str"]);
}
return ;
} 高级用法:http://www.thinksaas.cn/group/topic/347021/