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

标签 ios objective-c swift uitableview xcode8

我正在将 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/

相关文章:

ios - 在 xcode 中运行单独的 View / Controller

ios - 在 Swift map 上显示路线

iphone - TTStyledTextLabel 的 url 字体

iOS 8 NSInternalInconsistencyException

ios - 从 Objective-C 调用 Swift 扩展函数的语法

ios - 过滤器后未调用 cellForRowAtIndexPath

ios - Facebook 登录总是返回为已取消。 (iOS swift )

android - iOS 中用于渐进式 Web 应用程序的推送通知

ios - avplayer 在模拟器上而不是在设备上播放视频?

ios - 禁用 UI 旋转时 AVCaptureVideoPreviewLayer 方向错误