gpt4 book ai didi

ios - 使用 super View 从 Swift 获取 UITableViewCell 到 Objective-C

转载 作者:行者123 更新时间:2023-11-30 12:39:36 25 4
gpt4 key购买 nike

我正在将 Objective-C 项目迁移到 Swift 3。但我遇到了一个小问题。如何从 Objective-C 代码中调用这个 Swift 函数?
P.S:我已经在我的 .m 文件中导入了“project_name-Swift.h”。我的 Swift 代码:

func findSuperViewWithClass<T>(superViewClass : T.Type) -> UIView? {

var xsuperView : UIView! = self.superview!
var foundSuperView : UIView!

while (xsuperView != nil && foundSuperView == nil) {

if xsuperView.self is T {
foundSuperView = xsuperView
} else {
xsuperView = xsuperView.superview
}
}
return foundSuperView
}

我的 objective-c 代码:

UIButton * phoneButton = (UIButton *) sender;
UITableViewCell *cell = (UITableViewCell*) [phoneButton findSuperViewWithClass:[UITableViewCell class]];

并且它在“phoneButton findSuperViewWithClass”这一行的 objective-c 代码中显示ARC语义问题错误,如下所示 -> UIButton没有可见的@interface声明选择器“findSuperViewWithClass”

最佳答案

泛型的使用很可能导致 Swift 到 Objective-C 的桥接变得麻烦。
另请注意,桥接到 Objective-C 时,参数是方法名称的一部分。
尝试重构您的 Swift 函数以避免泛型并拥有更像 Swift 的签名(并避免修改您的 Objective-C 代码):

func findSuperView(class: AnyClass) -> UIView? {

/* ... */

if xsuperView.isKind(of: `class`) { // you can use a reserved keyword as a parameter name as long as you refer to it using `backticks`.
/* ... */
}

/* ... */
}

关于ios - 使用 super View 从 Swift 获取 UITableViewCell 到 Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42386077/

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