Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to change the return value (for object) with Aspects's AspectPositionInstead
@interface MyObject : NSObject
@property NSString *name;
@implementation MyObject
@interface AppDelegate ()
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSError *error = nil;
[self aspect_hookSelector:@selector(getMyObject) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> info){
NSInvocation *invocation = info.originalInvocation;
__unsafe_unretained MyObject *original = nil;
[invocation invoke];
[invocation getReturnValue:&original];
NSAssert([ isEqualToString:@"AAAAA"], @"");
MyObject *new = [[MyObject alloc] init]; = @"BBBBB";
objc_setAssociatedObject(invocation, _cmd, new, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[invocation setReturnValue:&new];
} error:&error];
for (int i = 0; i < 100; i++) {
MyObject *a = [self getMyObject];
NSAssert([ isEqualToString:@"BBBBB"], @"");
return YES;
- (MyObject*)getMyObject
MyObject *a = [[MyObject alloc] init]; = @"AAAAA";
return a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.