0

I have an enum defined in Objective C and used as a datatype in a Swift object.

Now I'm using that swift object in Objective C and I'd like to access the property.

However the property isn't added to the "xxxx-Swift.h" file in the class. I know it's possible to use Swift enums in Obj C with @objc prepending it but I don't have much control over this enum since it's auto generated from existing Objective C code.

2
  • if it's an Objective-C enum just make sure it's typedefed as as an NS_ENUM Commented Apr 12, 2016 at 2:21
  • Possible duplicate of Swift and comparing C typedef enums Commented Apr 12, 2016 at 2:22

1 Answer 1

1

I'm not sure exactly what your use case is, but here is a quick example of how an enum defined in Objective-C can be used in a Swift class, which can in turn be used in Objective-C.

Here is the Objective-C header (oclib.h):

#ifndef oclib_h
#define oclib_h

#import <Foundation/Foundation.h>

typedef enum {A, B, C} oc_enum_t;

void useSwiftClassInC();

#endif /* oclib_h */

Here is the corresponding Objective-C .m file (oclib.m):

#import "oclib.h"
#import "swift_c_1-Swift.h" // Need this to have access to Swift types; the swift_c_1 part will be your product module name.

void useSwiftClassInC() {
    UseEnum *ue = [[UseEnum alloc] initWithE:B i:444];
    printf("In Objective-C useSwiftClassInC(): m_Enum = %d, m_Int = %d\n", ue.m_Enum, ue.m_Int);
    ue.m_Enum = A;
    ue.m_Int = 10;
    [UseEnum printUseEnum: ue];
}

And here is the Swift file:

// Swift class that uses an enum from Objective-C    
class UseEnum : NSObject // NSObject inheritance is important!
    {
        var m_Enum : oc_enum_t
        var m_Int : Int32

        init(e : oc_enum_t, i : Int32)
        {
            m_Enum = e
            m_Int = i
        }

        static func printUseEnum( x : UseEnum )
        {
            print("In Swift UseEnum::printUserEnum: m_Enum = \(x.m_Enum), m_Int = \(x.m_Int)")
        }
    }

    // This calls an Objective-C function that does the following:
    // - creates a UseEnum object
    // - prints it out from Objective-C
    // - modifies it
    // - calls printUseEnum(), implemented in Swift, to print the object
    useSwiftClassInC()

The bridging header just has

#import "oclib.h"

You have probably seen this documentation already, but if not, here it is: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

Please provide more details about your specific situation if this doesn't answer your question. Here is the output I get from the example:

In Objective-C useSwiftClassInC(): m_Enum = 1, m_Int = 444
In Swift UseEnum::printUserEnum: m_Enum = oc_enum_t(rawValue: 0), m_Int = 10

An interesting situation arises if a data type defined in Swift is used in the signature of an Objective-C function that needs to be called from Swift. Xcode won't let us import the *-Swift.h into an Objective-C header or into a bridging header. A solution is to use @class forward declaration of the Swift type. Thus, if in the above example we needed an Objective-C function that takes UseEnum as a parameter, we could have something like this in oclib.h:

...
@class UseEnum; // Forward declaration must come before UseEnum is used!
...
void useSwiftClassInC_1(UseEnum * useEnum);
...
Sign up to request clarification or add additional context in comments.

2 Comments

Oh interesting, you made a wrapper class. Here's my use case: So there's an Objective C library with some enum. Call it ObjCLib. That objective C library is used in a Swift library. Call it SwiftLib. The Swift library has a settings class with one property being the enum type. I have a project written in Objective C that uses SwiftLib. I want to set the value of an enum property in the settings class in SwiftLib but the property doesn't get added to SwiftLib-Swift.h like all the other properties do since the data type isn't back compatible with Objective C.
Expanded the answer to include forward declarations, they are also described in the referenced Apple docs. Now you should be well equipped. All the best to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.