2

I am trying to use a objective c method with enum as argument in swift.The value for the argument is set based upon swift enum variable.

Swift enum

enum SecurityType: Int {

    case pushNotification = 0
    case touchId = 1
    case faceId = 2
}

My enum in objective c file look like

typedef NS_ENUM(NSUInteger, ScreenType) {
TouchID = 1,
FaceID = 2,
ConsentApproval = 3,
VerifyMyIdentity = 4 };

My swift code is

let screenType: ScreenType = self.biometricType == .touchId ? .touchID : .faceID

guard let newVC = MyViewController.init(screenType: screenType) else { return }

In above method biometricType variable is of swift enum type.

Here is my init method

-(instancetype)initWithScreenType:(screenType *)type {

self = [super init];

  if (self) {
     UIStoryboard *passcodeStoryBoard = [UIStoryboard  storyboardWithName:passcode bundle:nil];
     self = [passcodeStoryBoard  instantiateViewControllerWithIdentifier:@"AuthenticationViewController"];
     self.screenType = type;
     return self;
  }

return nil; }

I am getting an error on init method

Cannot convert value of type 'EnumType' to expected argument type 'UnsafeMutablePointer!'

Can somebody have any idea what might be the reason behind it.

3
  • 2
    Could you post how the init method is defined, thats half the story. Commented Aug 24, 2018 at 17:33
  • 1
    -(instancetype)initWithScreenType:(screenType *)type { => -(instancetype)initWithScreenType:(screenType)type { without the * Commented Aug 25, 2018 at 6:49
  • What a dumb mistake I've made here. Thanks a lot @Larme, it solved my problem. Commented Aug 27, 2018 at 6:54

1 Answer 1

0

First, the type of self.biometricType is UnsafeMutablePointer. You can't set UnsafeMutablePointer type to enum ScreenType.

Second, you should use full enum name to get access to obj-c enum like this example.

let screenType: ScreenType = ScreenType.TouchId
Sign up to request clarification or add additional context in comments.

2 Comments

I got your point but is there any workaround for using objective type enums. Also I can not changes objective c enum as it is a part of legacy code.
Did you create a bridging-header file with your obj-c headers to use obj-c file in swift?

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.