Home > database >  forward declare swift enumeration in objective c
forward declare swift enumeration in objective c

Time:09-06

I have a swift library that is used by a project with both objective-c and swift.

When using a class from the swift library in an objective-c header file, I can just forward declare the swift class, like this:

//SomeViewClass.h
#import <UIKit/UIKit.h>

//forward declaration of the swift class
@class SwiftClass

@interface SomeViewClass : UIView

@property (nonatomic, strong) SwiftClass *swiftInstance;

@end

How to do the same for an enumeration declared in the swift libary? Let's say i have this swift enumeration:

@objc public enum DtoMusicSelectionType: Int, RawRepresentable, Codable {
    case MusicChannel, Calendar
    
    public typealias RawValue = String

    public var rawValue: RawValue {
        switch self {
            case .MusicChannel:
                return "MusicChannel"
            case .Calendar:
                return "Calendar"
        }
    }

    public init?(rawValue: RawValue) {
        switch rawValue {
            case "MusicChannel":
                self = .MusicChannel
            case "Calendar":
                self = .Calendar
            default:
                return nil
        }
    }
}

How can I use that in the SomeViewClass.h? I want to do something like:

//SomeViewClass.h
#import <UIKit/UIKit.h>

//forward declaration of the swift class
@class SwiftClass
//How to forward declare an enum???
@enum DtoMusicSelectionType //this does not work

@interface SomeViewClass : UIView

@property (nonatomic) DtoMusicSelectionType musicSelectionType;
@property (nonatomic, strong) SwiftClass *swiftInstance;

@end

CodePudding user response:

You can forward declare with typedef NS_ENUM without a body, like:

typedef NS_ENUM(NSInteger, DtoMusicSelectionType);
  • Related