Vaibhav
Vaibhav

Reputation: 865

Calling objective c method with enum as argument in swift

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.

Upvotes: 2

Views: 557

Answers (1)

Alex Kolovatov
Alex Kolovatov

Reputation: 879

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

Upvotes: 0

Related Questions