iOS KVC valueForKeyPath学习心得

#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 0;
}
 

 

高级用法:http://www.thinksaas.cn/group/topic/347021/

 

posted @ 2016-01-16 14:06  Ficow  阅读(248)  评论(0编辑  收藏  举报