简体   繁体   中英

NS_ENUM in Swift class as a property

I've created an Objective-C enum in EnumsHeader.h file and imported that into <Module>-Bridging-Header.h #import "EnumsHeader.h" . I'm able to create a property of this enum type in a Swift class ( ClassA.Swift ). When I refer to this property from ClassB.m (Objective-C) class, I'm seeing compilation error: Property 'optionsFromA' not found on object of type 'ClassA *'

Am I missing anything here ?

EnumsHeader.h

#ifndef EnumsHeader_h
#define EnumsHeader_h

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, EnumOption) {
    EnumOptionA,
    EnumOptionB,
    EnumOptionC
};

#endif

ClassA.swift

public class ClassA: NSObject {
    public var optionsFromA: EnumOption!
}

ClassB.m

#import <Foundation/Foundation.h>
#import "TestEnumsInterop-Swift.h"

@class ClassB;

@interface ClassB:NSObject

@end

@implementation ClassB

- (instancetype)init {
    if (self = [super init]) {

        ClassA *a = [ClassA new];
        a.optionsFromA = EnumOptionA; //<--- Property 'optionsFromA' not found on object of type 'ClassA *'

    }
    return self;
}

@end

Objective-C has no Optional Value, You can declare the optionsFromA as:

public class ClassA: NSObject {
    public var optionsFromA: EnumOption = .A
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM