Created
January 13, 2015 10:46
-
-
Save sunnyxx/ffa9943fda7126cfded5 to your computer and use it in GitHub Desktop.
objc runtime block implementation with NS_REPLACES_RECEIVER
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
// Problem: | |
// I want to hack -awakeAfterUsingCoder: which has an attribute "NS_REPLACES_RECEIVER" | |
// Now I have a block version implmentation and to be added into UIView class | |
// It works fine in ARC with right object ownership | |
// But Xcode gives me a warning "ns_consumes_self attribute only applies to methods" | |
id (^newIMPBlock)(id, NSCoder *) NS_REPLACES_RECEIVER = | |
^id (UIView *self, __unused NSCoder *decoder) NS_REPLACES_RECEIVER { | |
// Do my staff | |
}; | |
// Add new method to UIView | |
SEL selector = @selector(awakeAfterUsingCoder:); | |
Method method = class_getInstanceMethod([UIView class], selector); | |
IMP newIMP = imp_implementationWithBlock(newIMPBlock); | |
const char *typeEncoding = method_getTypeEncoding(method); | |
class_addMethod([UIView class], selector, newIMP, typeEncoding); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment