简体   繁体   English

具有多个参数的函数的选择器

[英]Selector for a function with multiple parameters

I need my app extension to open my host app with a parameter.我需要我的应用程序扩展来打开带有参数的主机应用程序。 I do so by opening a custom URL with UIApplication.shared .我通过使用UIApplication.shared打开自定义URL来做到这一点。

Now.现在。 My last build has been rejected in test flight with a message "Need test for export compliance" or something like that (iTunes connect does not want to run in English).我的最后一个版本在试飞中被拒绝,并显示一条消息"Need test for export compliance"或类似的信息(iTunes connect 不想以英语运行)。 So I think that Require only App-Extension-Safe API set to NO in my extension's Build Settings is a possible reason of the problem.因此,我认为在我的扩展程序的Build SettingsRequire only App-Extension-Safe API设置为NO是问题的可能原因。

Setting the flag to YES disables the use of UIApplication.shared .将该标志设置为YES将禁用UIApplication.shared So I've decided to use the old way I found here some time ago (unfortunately, can't find the record):所以我决定使用我前段时间在这里找到的旧方式(不幸的是,找不到记录):

// Get "UIApplication" class name through ASCII Character codes.
NSString *className = [[NSString alloc] initWithData:[NSData dataWithBytes:(unsigned char []){0x55, 0x49, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E} length:13] encoding:NSASCIIStringEncoding];
if (NSClassFromString(className))
{
    //  A different way to call [UIApplication sharedApplication]
    id object = [NSClassFromString(className) performSelector: @selector(sharedApplication)];
    //  These lines invoke selector with 3 arguments
    SEL selector = @selector(openURL:options:completionHandler:);
    id  (*method)(id, SEL, id, id, id) = (void *)[object methodForSelector: selector];
    method(object, selector, myURL, nil, nil);
    //  Close extension
    [self.extensionContext completeRequestReturningItems: @[] completionHandler: nil];
}

As you can see, this is Objective-C , but I also need to make it Swifty .如您所见,这是Objective-C ,但我还需要将其设为Swifty So here's where I'm stuck:所以这就是我卡住的地方:

let codes = [CUnsignedChar](arrayLiteral: 0x55, 0x49, 0x41, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E)
let className = String(data: Data(bytes: UnsafeRawPointer(codes), count: 13), encoding: String.Encoding.ascii) ?? ""
if NSClassFromString(className) != nil
{
    let object = NSClassFromString(className)?.shared()
    let sel = #selector(_:options:completionHandler:)
    let meth = object?.method(for: sel)
    ...

The problem is with sel (expected expression).问题出在sel (预期表达式)上。 The method I am trying to reference here is我在这里尝试引用的方法是

open(_:options:completionHandler:) 

This thing这东西

#selector(open(_:options:completionHandler:))

does not work either(use of unresolved identifier) since the view controller I am implementing this code in has no such method.也不起作用(使用未解析的标识符),因为我在其中实现此代码的视图控制器没有这样的方法。

So the question is: how do I create a nameless selector reference with a few parameters to use later with my meth object?所以问题是:如何创建一个带有一些参数的无名选择器引用,以便稍后与我的meth对象一起使用? Thanks.谢谢。

You can use the NSSelectorFromString method which allows you to declare Selector instances using a String .您可以使用NSSelectorFromString方法,该方法允许您使用String声明Selector实例。

I don't have the full source code but with a few adjustments, the following should work:我没有完整的源代码,但经过一些调整,以下应该可以工作:

NSSelectorFromString("open(_:options:completionHandler:)")

More documentation on the method is available here .有关该方法的更多文档可在此处获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM