Skip to content

Instantly share code, notes, and snippets.

@sunnyxx
Created January 13, 2015 10:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnyxx/ffa9943fda7126cfded5 to your computer and use it in GitHub Desktop.
Save sunnyxx/ffa9943fda7126cfded5 to your computer and use it in GitHub Desktop.
objc runtime block implementation with NS_REPLACES_RECEIVER
// 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