如何将快速结构/枚举合并到Objective-C中?

时间:2022-09-07 09:42:35

Scenario: I've exposed the Objective-C file to Swift via the .h bridge; so I can launch the Objective-C via Storyboard from .Swift.

场景:我通过.h桥接向Swift公开了Objective-C文件;我可以从。swift通过Storyboard启动Objective-C。

However, I have some global enum & structs declared in Environment.Swift file that ObjC needs:

但是,我在环境中声明了一些全局enum & struct。ObjC需要的Swift文件:

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

Accessing enum within 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)
    }

I want to access this (and other data types) in an Objective-C file:

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

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

Xcode doesn't recognize this.

Xcode不承认这一点。

How do I expose environment data types (or can I?) defined in a .Swift file to an Objective-C file?
...and, is it possible to interpret a .Swift's struct in Objective-C? 如何将快速结构/枚举合并到Objective-C中?

如何将.Swift文件中定义的环境数据类型公开给Objective-C文件?…那么,是否有可能在Objective-C中解释。swift的结构呢?

1 个解决方案

#1


6  

There are a number of Swift features that can't be used in Objective-C. I posted an answer to a question the other day that asked something similar. I'll re-post the answer here:

有许多Swift特性不能在Objective-C中使用。有一天,我贴出了一个类似问题的答案。我将在这里重新发布答案:

According to Apple's docs on Swift and Objective-C compatibility:

根据苹果公司关于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:

只要与Objective-C兼容,就可以访问带有@objc属性的类或协议中的任何内容。这就排除了只有swift的特性,比如这里列出的:

  • Generics
  • 泛型
  • Tuples
  • 元组
  • Enumerations defined in Swift
  • 枚举中定义的迅速
  • Structures defined in Swift
  • 结构中定义的迅速
  • Top-level functions defined in Swift
  • 在Swift中定义的*函数
  • Global variables defined in Swift
  • Swift中定义的全局变量
  • Typealiases defined in Swift
  • 中定义typealias迅速
  • Swift-style variadics
  • Swift-style可变
  • Nested types
  • 嵌套类型
  • Curried functions
  • 咖喱功能

#1


6  

There are a number of Swift features that can't be used in Objective-C. I posted an answer to a question the other day that asked something similar. I'll re-post the answer here:

有许多Swift特性不能在Objective-C中使用。有一天,我贴出了一个类似问题的答案。我将在这里重新发布答案:

According to Apple's docs on Swift and Objective-C compatibility:

根据苹果公司关于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:

只要与Objective-C兼容,就可以访问带有@objc属性的类或协议中的任何内容。这就排除了只有swift的特性,比如这里列出的:

  • Generics
  • 泛型
  • Tuples
  • 元组
  • Enumerations defined in Swift
  • 枚举中定义的迅速
  • Structures defined in Swift
  • 结构中定义的迅速
  • Top-level functions defined in Swift
  • 在Swift中定义的*函数
  • Global variables defined in Swift
  • Swift中定义的全局变量
  • Typealiases defined in Swift
  • 中定义typealias迅速
  • Swift-style variadics
  • Swift-style可变
  • Nested types
  • 嵌套类型
  • Curried functions
  • 咖喱功能