Objective-C enum not showing up in Swift

I have an enum like this in my objective-c header file:

typedef NS_ENUM(NSInteger, FontSize) {
    VerySmall = 12,
    Small = 14,
    Medium = 16,
    Big = 18
};

      

Then in my bridge header, I import that header.

from my swift code, when I try to declare "FontSize" as a parameter, the compiler says "Use undeclared font FontSize".

From the developer guide, this should be possible. Is anyone experiencing the same problem?

+3


source to share


3 answers


Start with a clean Swift project , add one file .h

(Accept automatic generation of bridge headers)

Objective-C FontSize.h

typedef NS_ENUM(NSInteger, FontSize) {
    VerySmall = 12,
    Small = 14,
    Medium = 16,
    Big = 18
};

      

Bridging header

#import "FontSize.h"

      



Fast implementation

import UIKit
class ViewController: UIViewController {
    let fontSize:FontSize = .VerySmall
}

      


Built, linked, launched and tested on Xcode 6.4 and 7.0.

+4


source


I had the same problem and resolved it by doing BOTH of the following:

  • Move enum declaration outside of @interface field
  • Delete the period '.' from enum link in Swift code


let fontSize:FontSize = VerySmall

+2


source


I still couldn't see my enums even with NS_ENUM's answer above.

It turns out there were changes in XCode 7.3 where NS_ENUM must be defined outside of the @ interface- @end block.

Call obj-c enum from swift work without upgrading to Xcode 7.3 swift 2.2

+1


source







All Articles