将Swift 3升级到4,不再在目标C中进行Swift扩展 [英] Upgrading Swift 3 to 4, swift extension no longer in objective c

查看:148
本文介绍了将Swift 3升级到4,不再在目标C中进行Swift扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚完成了将混合语言项目(objective-c和Swift)从Swift 3升级到Swift4.

I just finished upgrading a mixed language project (objective-c and Swift) from Swift 3 to Swift 4.

除了我的所有Swift扩展都无法在Objective-C中访问之外,一切似乎都进行得很好.我不知道如何获得 any Swift扩展以显示在Objective-C中.我已经尝试过搜索,但是除了放松private范围之外,我几乎没有提到对Swift 4扩展所做的任何更改.

Everything seemed to go well except all of my Swift extensions are no longer accessible in objective-c. I can't figure out how to get any Swift extension to show up in objective-c. I've tried searching, but I can't find any mention of changes to extensions in Swift 4 except for loosening up the private scope.

  • 所有这些扩展都可以从Swift 3的Objective-c中访问,因此没有不兼容的类型(结构或非原始枚举).
  • 扩展名标记为公开.
  • 扩展名是与Objective-C文件相同的目标,并且在同一项目中.
  • 是的,我已经在相关的Objective-c文件中导入了"ProjectName-Swift.h".
  • 出现其他兼容的Swift类 do .似乎只是扩展名丢失了.
  • 我也尝试过将每个func标记为公开.
  • All of these extensions were accessible from Objective-c in Swift 3, so there are no incompatible types (structs or non-raw enums).
  • The extensions are marked public.
  • The extensions are part of the same target and in the same project as the objective-c files.
  • Yes, I have imported "ProjectName-Swift.h" in the relevant objective-c files.
  • Other compatible Swift classes do show up. It seems just the extensions are missing.
  • I've tried marking each func public as well.

例如,当我使用Swift 3时,以下扩展曾经可用于Objective-C代码:

For example, the following extension used to be available to objective-c code when I was using Swift 3:

public extension UIAlertController {

  class func alert(_ title: String? = nil, message: String? = nil) -> UIAlertController {
    return UIAlertController(title: title, message: message, preferredStyle: .alert)
  }

  @discardableResult func action(_ action: String) -> UIAlertController {
    self.addAction(UIAlertAction(title: action, style: .default, handler: nil))
    return self
  }

  @discardableResult func action(_ action: String, style: UIAlertActionStyle = .default, onSelected: ((UIAlertAction) -> Void)? = nil) -> UIAlertController {
    self.addAction(UIAlertAction(title: action, style: style, handler: onSelected))
    return self
  }

  @discardableResult func cancel(_ action: String? = "Cancel", onCancel: ((UIAlertAction) -> Void)? = nil) -> UIAlertController {
    self.addAction(UIAlertAction(title: action, style: .cancel, handler: { alert in
      onCancel?(alert)
    }))
    return self
  }

  @discardableResult func destructive(_ action: String, onDestruct: @escaping ((UIAlertAction) -> Void)) -> UIAlertController {
    self.addAction(UIAlertAction(title: action, style: .destructive, handler: { action in
      onDestruct(action)
    }))
    return self
  }

  func presentOn(_ viewController: UIViewController, animated: Bool = true) {
    viewController.present(self, animated: animated, completion: nil)
  }
}

推荐答案

一个超级简单的解决方案是将@objc public extension添加到每个Swift扩展的声明中.

A super simple solution is to just add @objc public extension to the declaration of each Swift extension.

这篇关于将Swift 3升级到4,不再在目标C中进行Swift扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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