BalestraPatrick BalestraPatrick - 7 months ago 106
Swift Question

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?

Answer

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.

Comments