ios – 数组中的Swift闭包在Objective-c中变为nil

前端之家收集整理的这篇文章主要介绍了ios – 数组中的Swift闭包在Objective-c中变为nil前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了一个 objective-c方法,它将通过NSInvocation调用一个方法
typedef void (^ScriptingEmptyBlock)();
typedef void (^ScriptingErrorBlock)(NSError *error);
- (void)scripting_execute:(NSString *)operation withParams:(nullable NSArray *)args {

    SEL selector = [self scripting_selectorForOperation:operation];
    Class class = [self class];
    NSMethodSignature *signature = [class instanceMethodSignatureForSelector:selector];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

    [invocation setSelector:selector];
    [invocation setTarget:self];
    for (int idx = 0; idx < args.count; idx ++) {
        id arg = args[idx];
        [invocation setArgument:&arg atIndex:idx + 2];

    }

    ScriptingEmptyBlock success = args[1];

    // Breakpoint added on next line to test for nil
    success(); // this is nil and would crash!
    // (lldb) po args.count
    // 3

    // (lldb) po success
    // Printing description of success:
    // (ScriptingEmptyBlock) success = 0x0000000000000000

    // (lldb) po args[1]
    // (Function)

    //[invocation getArgument:&success atIndex:2]; // also tried this and got nil as well
    [invocation invoke];

}

方法采用“操作”,通过在子类中重写scripting_selectorForOperation来转换为选择器,然后执行调用.

所有这些都有效,除非被调用方法具有块参数,它们都是nil,我添加了我用注释描述的nil的测试,当试图从数组中读取闭包时它将是nil.

叫做:

let successClosure: ScriptingEmptyBlock = {
                    print("Renamed product")
                }
let errorClosure: ScriptingErrorBlock = { error in
                    print("Failed to rename product: \(error)")
                }
let params:[Any] = [ "testName",successClosure,errorClosure]
object.scripting_execute (ScriptOperation.updateProductName.rawValue,withParams: params)

封闭为什么变成零?

解决方法

成功不是零(事实上,NSArray不能包含nils).如果你打印它像NSLog(@“%@”,成功);它会说(函数),而不是(null).如果你像NSLog(@“%@”,[成功类])那样打印它的类,它会说_SwiftValue.基本上,它是Swift值,桥接到Objective-C.

问题是对象成功指向的不是Objective-C块.它是一个Swift闭包,Swift闭包与Objective-C块不同.尝试使用调用它就好像它是一个Objective-C块导致未定义的行为.调试器中的po打印错误可能是因为它正在打印它,假设它是类型ScriptingEmptyBlock(块类型).如果你做po(id)成功,它将打印(功能).

至于如何从Swift中明确地将Objective-C块放入数组中,我想出的唯一方法就是:

let params:[Any] = [ "testName",successClosure as (@convention(block) () -> Void)!,errorClosure as (@convention(block) (NSError) -> Void)!]
object.scripting_execute (ScriptOperation.updateProductName.rawValue,withParams: params)

我不确定为什么有必要将函数类型放在!中,但它似乎不起作用.也许其他人可以找到更好的方法.

原文链接:https://www.f2er.com/iOS/335325.html

猜你在找的iOS相关文章