Setting bitmask enum declared in objective-c from swift

1.7k views Asked by At

I'm trying to use Parse SDK for iOS in my new project. It has viewController with enum property;

typedef enum {
    PFLogInFieldsNone = 0,
    PFLogInFieldsUsernameAndPassword = 1 << 0,
    PFLogInFieldsPasswordForgotten = 1 << 1,
    PFLogInFieldsLogInButton = 1 << 2,
    PFLogInFieldsFacebook = 1 << 3,
    PFLogInFieldsTwitter = 1 << 4,
    PFLogInFieldsSignUpButton = 1 << 5,
    PFLogInFieldsDismissButton = 1 << 6,

    PFLogInFieldsDefault = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton |      PFLogInFieldsSignUpButton | PFLogInFieldsPasswordForgotten | PFLogInFieldsDismissButton
 } PFLogInFields;

According to tutorial in Objective-C I should set it in this way:

 [logInViewController setFields: PFLogInFieldsTwitter | PFLogInFieldsFacebook | PFLogInFieldsDismissButton];

I'm trying to do it in this way(using swift):

loginViewController.fields = PFLogInFieldsTwitter | PFLogInFieldsFacebook | PFLogInFieldsDismissButton

But I get error:"'PFLogInFields' is not convertible to 'Bool'"

So, what is the correct way to set such kind of properties?

3

There are 3 answers

2
dcgoss On BEST ANSWER

I had the same problem as you. See this answer for how to set PFLogInFields in Swift. It worked for me!!

0
Mundi On

In Swift you have to prefix the enums with the Type. I am not sure if this works automatically with Objective-C imports, but it might:

logInViewController.fields = PFLogInFields.PFLogInFieldsTwitter | ...

If the library were ported to Swift standard, the fields would already expect PFLoginFields and the enum items would be defined in a manner so that you can write

logInViewController.fields = .Twitter | .Facebook ...
0
newacct On

Consecutive enums in Objective-C should be refactored to use NS_ENUM, and bit-field enums should be refactored to use NS_OPTIONS.

You should change

typedef enum {
    //...
} PFLogInFields;

to

typedef NS_OPTIONS(NSInteger, PFLogInFields) {
    //...
};