BalestraPatrick
BalestraPatrick

Reputation: 10144

Objective-C enum is not visible in Swift

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

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

Then in my bridging header I import this header.

from my swift code, when I try to declare 'FontSize' as parameter, the compiler says 'Use of undeclared type FontSize'.

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

Upvotes: 4

Views: 3056

Answers (3)

danfordham
danfordham

Reputation: 978

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

  1. Move the enum declaration to outside the @interface block
  2. Remove the period '.' from the enum reference in the Swift code

let fontSize:FontSize = VerySmall

Upvotes: 5

David Boyd
David Boyd

Reputation: 6601

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

It turns out there was a change in XCode 7.3 where NS_ENUMs have to be defined outside the @interface-@end block.

Calling obj-c enum from swift not working after upgrading to Xcode 7.3 swift 2.2

Upvotes: 1

SwiftArchitect
SwiftArchitect

Reputation: 48514

Start over with a clean Swift project, add a single .h file (accept the automatic creation of Bridging-Headers)

Objective-C FontSize.h

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

Bridging-Header

#import "FontSize.h"

Swift Implementation

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

Built, linked, ran & tested on Xcode 6.4 & 7.0.

Upvotes: 4

Related Questions