Forked from vgrichina/NSObject+KVOWeakPropertyDebug.h
Created
September 10, 2013 13:46
-
-
Save th-in-gs/6509624 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// NSObject+KVOWeakPropertyDebug.h | |
// KVOWeakPropertyDebug | |
// | |
// Created by Vladimir Grichina on 12.01.13. | |
// Copyright (c) 2013 Vladimir Grichina. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSObject (KVOWeakPropertyDebug) | |
@end |
This file contains 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
// | |
// NSObject+KVOWeakPropertyDebug.m | |
// KVOWeakPropertyDebug | |
// | |
// Created by Vladimir Grichina on 12.01.13. | |
// Copyright (c) 2013 Vladimir Grichina. All rights reserved. | |
// | |
#import "NSObject+KVOWeakPropertyDebug.h" | |
#import <objc/runtime.h> | |
void MethodSwizzle(Class c, SEL origSEL, SEL overrideSEL) | |
{ | |
Method origMethod = class_getInstanceMethod(c, origSEL); | |
Method overrideMethod = class_getInstanceMethod(c, overrideSEL); | |
if (class_addMethod(c, origSEL, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) { | |
class_replaceMethod(c, overrideSEL, method_getImplementation(origMethod), method_getTypeEncoding(origMethod)); | |
} else { | |
method_exchangeImplementations(origMethod, overrideMethod); | |
} | |
} | |
@implementation NSObject (KVOWeakPropertyDebug) | |
+ (void)load | |
{ | |
MethodSwizzle(self, @selector(addObserver:forKeyPath:options:context:), @selector(_addObserver:forKeyPath:options:context:)); | |
} | |
- (BOOL)_isWeak:(NSString *)keyPath | |
{ | |
// TODO: Support complex keyPath variants | |
objc_property_t property = class_getProperty(self.class, keyPath.UTF8String); | |
if (property) { | |
return property_getAttributes(property)[3] == 'W'; | |
} | |
return NO; | |
} | |
- (void)_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context | |
{ | |
if ([self _isWeak:keyPath]) { | |
NSLog(@"WARNING: observing '%@' property, which is weak and so isn't fully KVO-compliant", keyPath); | |
} | |
[self _addObserver:observer forKeyPath:keyPath options:options context:context]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment