Frederick C. Lee
Frederick C. Lee

Reputation: 9493

How would you incorporate a Swift struct/enum into Objective-C?

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.

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

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:

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

Xcode doesn't recognize this.

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? enter image description here

Upvotes: 3

Views: 2664

Answers (1)

user887210
user887210

Reputation:

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:

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

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

Upvotes: 6

Related Questions