gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:19:42 25 4
gpt4 key购买 nike

我需要我的应用程序扩展来使用参数打开我的主机应用程序。为此,我使用 UIApplication.shared 打开自定义 URL

现在。我的最后一个构建在试飞中被拒绝,并显示消息 “需要测试导出合规性” 或类似的东西(iTunes 连接不想以英语运行)。因此,我认为在我的扩展程序的 Build Settings 中将 Require only App-Extension-Safe API 设置为 NO 可能是问题的原因。

将标志设置为 YES 将禁用 UIApplication.shared。所以我决定使用我前段时间在这里找到的旧方法(不幸的是,找不到记录):

// 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];
}

如您所见,这是 Objective-C,但我还需要将其设为 Swifty。所以这就是我被困的地方:

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)
...

问题在于 sel(预期表达式)。我在这里尝试引用的方法是

open(_:options:completionHandler:) 

这件事

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

也不起作用(使用未解析的标识符),因为我在其中实现此代码的 View Controller 没有这样的方法。

所以问题是:我如何创建一个带有一些参数的无名选择器引用,以便稍后与我的 meth 对象一起使用?谢谢。

最佳答案

您可以使用 NSSelectorFromString 方法,该方法允许您使用 String 声明 Selector 实例。

我没有完整的源代码,但经过一些调整,下面应该可以工作:

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

有关该方法的更多文档可用 here .

关于ios - 具有多个参数的函数的选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46236247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com