Created
October 28, 2013 10:07
-
-
Save wader/7194297 to your computer and use it in GitHub Desktop.
KVO test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 2013-10-28 11:05:58.558 ivar_kvo_test[17451:303] ivar = | |
| 2013-10-28 11:05:58.559 ivar_kvo_test[17451:303] _ivarWithManualSetter = | |
| 2013-10-28 11:05:58.559 ivar_kvo_test[17451:303] _prop = | |
| 2013-10-28 11:05:58.560 ivar_kvo_test[17451:303] _propWithSetter = | |
| 2013-10-28 11:05:58.560 ivar_kvo_test[17451:303] setValue:forKey:ivar | |
| 2013-10-28 11:05:58.561 ivar_kvo_test[17451:303] observeValueForKeyPath: ivar | |
| 2013-10-28 11:05:58.561 ivar_kvo_test[17451:303] setValue:forKey:ivarWithManualSetter | |
| 2013-10-28 11:05:58.561 ivar_kvo_test[17451:303] observeValueForKeyPath: ivarWithManualSetter | |
| 2013-10-28 11:05:58.561 ivar_kvo_test[17451:303] setValue:forKey:prop | |
| 2013-10-28 11:05:58.562 ivar_kvo_test[17451:303] observeValueForKeyPath: prop | |
| 2013-10-28 11:05:58.562 ivar_kvo_test[17451:303] setValue:forKey:propWithSetter | |
| 2013-10-28 11:05:58.562 ivar_kvo_test[17451:303] observeValueForKeyPath: propWithSetter | |
| 2013-10-28 11:05:58.563 ivar_kvo_test[17451:303] ivar has no setter | |
| 2013-10-28 11:05:58.563 ivar_kvo_test[17451:303] setIvarWithManualSetter: | |
| 2013-10-28 11:05:58.563 ivar_kvo_test[17451:303] observeValueForKeyPath: ivarWithManualSetter | |
| 2013-10-28 11:05:58.564 ivar_kvo_test[17451:303] setProp: | |
| 2013-10-28 11:05:58.564 ivar_kvo_test[17451:303] observeValueForKeyPath: prop | |
| 2013-10-28 11:05:58.564 ivar_kvo_test[17451:303] setterForPropWithSetter: | |
| */ | |
| #import <Foundation/Foundation.h> | |
| @interface Test : NSObject { | |
| NSString *_ivar; | |
| NSString *_ivarWithManualSetter; | |
| } | |
| @property(nonatomic, retain) NSString *prop; | |
| @property(nonatomic, retain, setter = setterForPropWithSetter:) NSString *propWithSetter; | |
| @end | |
| @implementation Test | |
| - (id)init { | |
| self = [super init]; | |
| if (self == nil) { | |
| return nil; | |
| } | |
| [self addObserver:self forKeyPath:@"ivar" options:0 context:NULL]; | |
| [self addObserver:self forKeyPath:@"ivarWithManualSetter" options:0 context:NULL]; | |
| [self addObserver:self forKeyPath:@"prop" options:0 context:NULL]; | |
| [self addObserver:self forKeyPath:@"propWithSetter" options:0 context:NULL]; | |
| NSLog(@"ivar = "); | |
| _ivar = @"a"; | |
| NSLog(@"_ivarWithManualSetter = "); | |
| _ivarWithManualSetter = @"a"; | |
| NSLog(@"_prop = "); | |
| _prop = @"a"; | |
| NSLog(@"_propWithSetter = "); | |
| _propWithSetter = @"a"; | |
| NSLog(@"setValue:forKey:ivar"); | |
| [self setValue:@"b" forKey:@"ivar"]; | |
| NSLog(@"setValue:forKey:ivarWithManualSetter"); | |
| [self setValue:@"b" forKey:@"ivarWithManualSetter"]; | |
| NSLog(@"setValue:forKey:prop"); | |
| [self setValue:@"b" forKey:@"prop"]; | |
| NSLog(@"setValue:forKey:propWithSetter"); | |
| [self setValue:@"b" forKey:@"propWithSetter"]; | |
| NSLog(@"ivar has no setter"); | |
| NSLog(@"setIvarWithManualSetter:"); | |
| [self setIvarWithManualSetter:@"c"]; | |
| NSLog(@"setProp:"); | |
| [self setProp:@"c"]; | |
| NSLog(@"setterForPropWithSetter:"); | |
| [self setterForPropWithSetter:@"c"]; | |
| return self; | |
| } | |
| - (void)setIvarWithManualSetter:(NSString *)ivarWithManualSetter { | |
| _ivarWithManualSetter = ivarWithManualSetter; | |
| } | |
| - (void)dealloc { | |
| [self removeObserver:self forKeyPath:@"ivar"]; | |
| [self removeObserver:self forKeyPath:@"ivarWithManualSetter"]; | |
| [self removeObserver:self forKeyPath:@"prop"]; | |
| [self removeObserver:self forKeyPath:@"propWithSetter"]; | |
| } | |
| - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { | |
| NSLog(@" observeValueForKeyPath: %@", keyPath); | |
| } | |
| @end | |
| int main(int argc, const char * argv[]) | |
| { | |
| (void)[[Test alloc] init]; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment