gpt4 book ai didi

objective-c - 你如何将 Swift 结构/枚举合并到 Objective-C 中?

转载 作者:搜寻专家 更新时间:2023-10-31 22:08:50 26 4
gpt4 key购买 nike

场景:我已经通过 .h 桥将 Objective-C 文件公开给 Swift;所以我可以通过 Storyboard 从 .Swift 启动 Objective-C。

然而,我在 ObjC 需要的 Environment.Swift 文件中声明了一些全局枚举和结构:

enum BarButtonItem:Int {
case cancel = 1
case back
case save
case activate
case upload
case share
}

在 Swift 中访问枚举:

  @IBAction func navButtonAction(sender: UIBarButtonItem) {
if let buttonItem = BarButtonItem(rawValue: sender.tag) {
switch buttonItem {
case .save:
println("{(3.3)AccessBoundaries} Save.")
default:
println("")
}
}
self.dismissViewControllerAnimated(true, completion: nil)
}

我想在 Objective-C 文件中访问这个(和其他数据类型):

- (IBAction)barButtonAction:(UIBarButtonItem *)sender {
BarButtonItem....?
}

Xcode 无法识别这一点。

如何将 .Swift 文件中定义的环境数据类型(或者我可以?)公开到 Objective-C 文件?
...并且,是否可以在 Objective-C 中解释 .Swift 的结构? enter image description here

最佳答案

有许多 Swift 特性无法在 Objective-C 中使用。我发布了一个answer to a question前几天问了类似的问题。我会在这里重新发布答案:

根据 Apple's docs关于 Swift 和 Objective-C 的兼容性:

You’ll have access to anything within a class or protocol that’s marked with the @objc attribute as long as it’s compatible with Objective-C. This excludes Swift-only features such as those listed here:

  • Generics
  • Tuples
  • Enumerations defined in Swift
  • Structures defined in Swift
  • Top-level functions defined in Swift
  • Global variables defined in Swift
  • Typealiases defined in Swift
  • Swift-style variadics
  • Nested types
  • Curried functions

关于objective-c - 你如何将 Swift 结构/枚举合并到 Objective-C 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27072302/

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