gpt4 book ai didi

objective-c - 无法从 Objective-C 调用 Swift 函数

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:01 25 4
gpt4 key购买 nike

我想在 Objective-C 中调用 Swift 类中的这个静态函数:

static internal func photoActionSheet<T:UIViewController where T:UIImagePickerControllerDelegate, T:UINavigationControllerDelegate>(hostAndDelegateViewController:T)

我可以从 ObjC 调用该类中的其他函数,但它不知道只有这个函数

谢谢

最佳答案

具有通用类型约束的函数无法导出到 Objective-C。

作为解决方法,您可以像下面这样定义函数并检查函数实现中的协议(protocol)一致性:

static internal func photoActionSheet(hostAndDelegateViewController: UIViewController) {
guard let vc = hostAndDelegateViewController as? protocol<UINavigationControllerDelegate, UIImagePickerControllerDelegate> else {
fatalError("hostAndDelegateViewController must conform UINavigationControllerDelegate and UIImagePickerControllerDelegate")
}

// let picker = UIImagePickerController()
// picker.delegate = vc
}

同样,Objective-C 类型 UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate> *将作为 UIViewController! 导出到 Swift

关于objective-c - 无法从 Objective-C 调用 Swift 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33888719/

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