从Bundle调用Swift函数 [英] Call Swift function from Bundle

查看:62
本文介绍了从Bundle调用Swift函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Xcode和Swift在自定义包中声明了一个函数。

I have declared a function in a custom bundle using Xcode and Swift.

MyBundle.bundle —> File.swift —> func… (){}

MyBundle.bundle —> File.swift —> func …() {}

如何使用 Foundation.Bundle

推荐答案

好吧,您不能直接调用它,但是可能可以采取一些措施来解决。

Well you can't call it directly but probably you can make some way around that might work.

您可以使用 NSClassFromString 从字符串创建该类的实例,然后使用该调用

You can create an instance of that class from string using NSClassFromString and then you call the method using that call

类似以下内容

func getClassName(_ strClassName: String) -> AnyClass! {
   let bundle = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String;
    let requiredClass: AnyClass = NSClassFromString("\(namespace).\(strClassName)")!;
    return requiredClass;
}

然后,您可以使用上述方法返回的类实例来调用该方法

Then you can call the method using your class instance returned by the above method like

yourClassInstance.perform(Selector("functionName"))

这篇关于从Bundle调用Swift函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆