NSInvocation 介绍和使用

发布于 2024-10-12 12:56:03 字数 5653 浏览 17 评论 0

当前环境: Xcode10.0 Swift4.2 iOS SDK 12.1

Demo:https://github.com/zColdWater/NSInvocationDemo

NSInvocation 做什么的?

NSInvocation 对象用于在对象之间和应用程序之间存储和转发消息,主要是通过 NSTimer 对象和分布式对象系统。一个 NSInvocation 对象包含一个 Objective-C 消息的所有元素:一个目标,一个选择器,参数和返回值。可以直接设置这些元素中的每一个,并在 NSInvocation 调度对象时自动设置返回值。

一个 NSInvocation 对象可以被反复派遣到不同的目标; 它的参数可以在发送之间修改以获得不同的结果; 甚至它的选择器也可以使用相同的方法签名(参数和返回类型)更改为另一个。这种灵活性 NSInvocation 有助于重复具有许多参数和变体的消息; 而不是为每条消息重新输入略有不同的表达式

默认情况下,此类不保留包含的调用的参数。如果这些对象可能在您创建实例 NSInvocation 的时间和使用它的时间之间消失,那么您应该自己显式保留对象或调用方法以使调用对象自己保留它们。retainArguments

NSInvocation 怎么用?

这里我上一段 Demo,或许你也可以在上面 Demo 的地址里面找到答案。

为了看下效果,我故意给 foo 函数包含 2 个参数,一个返回值。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // 创建 SEL 方法 二选一即可
//    SEL selector = @selector(foo::);
    SEL selector1 = NSSelectorFromString(@"foo::");

    //---------------- 开始 获取方法签名 ----------------//
    NSMethodSignature *sign = [self methodSignatureForSelector:selector1];
    NSLog(@"numberOfArguments:%lu",sign.numberOfArguments);
    NSLog(@"frameLength:%lu",sign.frameLength);
    NSLog(@"isOneway:%d",[sign isOneway]);
    NSLog(@"methodReturnType:%s",[sign methodReturnType]);
    NSLog(@"methodReturnLength:%lu",[sign methodReturnLength]);


    //---------------- 结束 获取方法签名 ----------------//

    // 通过 方法签名创建 Invocation 对象
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sign];

    // 方法选择器 必传参数
    invocation.selector = selector1;


    //---------------- 开始 设置函数参数 ----------------//
    // 备注: 索引 0 和 1 分别表示隐藏的参数 self 和_cmd; 可以使用 target 和 selector 方法直接检索这些值。对通常在消息中传递的参数使用索引 2 和更大。
    // 官方文档: https://developer.apple.com/documentation/foundation/nsinvocation/1437830-getargument?language=objc
    int arg2 = 10;
    [invocation setArgument:&arg2 atIndex:2];

    NSString* (^arg3)(NSString *)  = ^NSString* (NSString* va1)
    {
        return va1;
    };
    [invocation setArgument:&arg3 atIndex:3];
    //---------------- 结束 设置函数参数 ----------------//

    // 开始调用
    [invocation invokeWithTarget:self];


    //---------------- 开始 插入返回值 ----------------//
    NSString *setResult = @"[替换掉方法真正的返回值]";
    [invocation setReturnValue: &setResult];
    //---------------- 结束 插入返回值 ----------------//



    //---------------- 开始 得到函数返回值 ----------------//
    id result = nil;
    [invocation getReturnValue:&result];
    NSLog(@"方法返回值:%@",result);
    //---------------- 结束 得到函数返回值 ----------------//



    //---------------- 开始 查看参数 ----------------//
    id _arg0 = nil;
    [invocation getArgument:&_arg0 atIndex:0];
    NSLog(@"arg0:%@",_arg0);

    SEL _arg1 = nil;
    [invocation getArgument:&_arg1 atIndex:1];
    NSLog(@"arg1:%@",NSStringFromSelector(_arg1));

    int _arg2;
    [invocation getArgument:&_arg2 atIndex:2];
    NSLog(@"arg2:%d",_arg2);

    id _arg3 = nil;
    [invocation getArgument:&_arg3 atIndex:3];
    NSLog(@"arg3:%@",_arg3);
    //---------------- 结束 查看参数 ----------------//


//    NSLog(@"<%@:%@:%d>", NSStringFromClass([self class]), NSStringFromSelector(_cmd), __LINE__);

}


//MARK: - 测试函数
- (id)foo: (int) count
                  : (NSString* (^)(NSString *)) block {
    NSLog(@"personalMethod param1:%d, param2:%@",count,block(@"参数 2"));
    return @"[真返回值]";
}

@end

补充

通过类名,创建类对象并且调用其内部方法。该如何实现呢?

创建 demo 类 FooObject

FooObject.h 文件

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface FooObject : NSObject

- (instancetype)initWithWebView:(UIView *)view;

- (void)sayHello;

@end

NS_ASSUME_NONNULL_END

FooObject.m 文件

#import "FooObject.h"
#import <UIKit/UIKit.h>

@implementation FooObject

- (instancetype)initWithWebView:(UIView *)view
{
    self = [super init];
    if(self) {
        NSLog(@"initWithWebView_init: %@", self);
    }
    return self;
}

- (void)sayHello {
    NSLog(@"sayHello");
}
@end

在外部通过类名调用 FooObject 对象

这里有个插曲 再动态拿这个函数的返回结果的时候 Crash 了,经过 Google 找到了解决办法,下面给了答案地址和解释。

// Invoke method id expectInstance = [self dynamicCreateInstance];
SEL sayhello = NSSelectorFromString(@"sayHello");
[expectInstance performSelector:sayhello withObject:nil];
(id)dynamicCreateInstance {
  id fooClass = [NSClassFromString(@"FooObject") alloc];
  SEL fooInitSel = NSSelectorFromString(@"initWithWebView:");
  NSMethodSignature fooSign = [fooClass methodSignatureForSelector:fooInitSel];
  NSInvocation fooInvocation = [NSInvocation invocationWithMethodSignature:fooSign];
  fooInvocation.selector = fooInitSel;
  UIView *webview = [[UIView alloc] init];
  [fooInvocation setArgument:&webview atIndex:2];
  [fooInvocation invokeWithTarget:fooClass];
  // prevent crash // stackoverflow:  https://stackoverflow.com/questions/22018272/nsinvocation-returns-value-but-makes-app-crash-with-exc-bad-access 
  id __unsafe_unretained tempResultSet;
  [fooInvocation getReturnValue:&tempResultSet];
  id resultSet = tempResultSet;
  // id fooResult = nil;
  // [fooInvocation getReturnValue:&fooResult];
  // NSLog(@"FooObject 对象实例:%@",fooResult);
  return resultSet;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

仲春光

暂无简介

文章
评论
27 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文