oc 키 감청

1597 단어 Objective-C
//
//  main.m
//  testt
//
//  Created by Young on 15/9/11.
//  Copyright (c) 2015  Young. All rights reserved.
//

#import <Foundation/Foundation.h>





@interface GetSet : NSObject
{
    //{ }             , @property  
    
}
//@property           ,                @synthesize  

@property NSString* str; //  1
@property NSString* ss;  //  2

@end




@implementation GetSet

@synthesize str=myStr; //    MyStr,  str          MyStr
                    //ss      ,            _ss

-(void)show{
    
    //                  
    [self addObserver:self  forKeyPath:@"str" options:NSKeyValueObservingOptionNew   context:nil];
    
  
}
// str            
 -(void )observeValueForKeyPath:(NSString *)keyPath ofObject:(id)
    object change:(NSDictionary *)change context:(void *)context{
    
    NSLog(@"    %@  %@   %@",keyPath, object, [change objectForKey:@"new"]);

}

-(void)dealloc{
//    
    [self removeObserver:self forKeyPath:@"str" ];
}
//-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
//
//}
-(id)valueForUndefinedKey:(NSString *)key{
    return nil;
}


@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        GetSet* gs=[[GetSet alloc]init];
        
        [gs show]; //    
        [gs setValue:@"iii" forKey:@"myStr"];  //         
        [gs setStr:@"ooo"];//       
    }
    return 0;
}

인쇄 하 다.
2015-09-19 16:29:30.438 testt[1877:100382]     str  <GetSet: 0x100206a20>   ooo

좋은 웹페이지 즐겨찾기