18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftとObjective-Cでenumの相互利用

Posted at

SwiftでObjective-Cのenumを使う

enumではなく、NS_ENUM()またはNS_OPTIONS()で定義する。

ColorType.h
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSUInteger, ColorType) {
    ColorTypeRed,
    ColorTypeGreen,
    ColorTypeBlue,
    ColorTypeAlpha,
};

Swiftで利用するために、ヘッダファイルをBridging Headerでimportする。

Bridging-Header.h
#import "ColorType.h"

Build SettingsでBridging Headerの指定を忘れずに。

BuildSettings_BridgeingHeader.png

Swiftのenumと同様に使える。

AnyClass.swift
class AnyClass {
    func test(type: ColorType) {
        switch type {
        case .Red:
            NSLog("Red");
        default:
            NSLog("Others");
        }
    }
}

Objective-CでSwiftのenumは使えない

18
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?