Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Could not find an overload for '|' that accepts the supplied arguments

Trying to incorporate Parse into a new Swift project.

When I get to this block:

logInViewController.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton | PFLogInFieldsSignUpButton | PFLogInFieldsPasswordForgotten

I'm getting this error in XCode 6:

Could not find an overload for '|' that accepts the supplied arguments

Anyone happen to know what's wrong with that syntax?

like image 345
Z Jones Avatar asked Jun 10 '14 03:06

Z Jones


4 Answers

Use the .value then use the result to create a PFLogInFields instance:

logInViewController.fields = PFLogInFields(PFLogInFieldsUsernameAndPassword.value 
    | PFLogInFieldsLogInButton.value)
like image 50
Timothy Walters Avatar answered Sep 30 '22 12:09

Timothy Walters


Timothy's answer is right but it is better to correct the code with Swift's update.

logInViewController.fields = PFLogInFields(rawValue: 
PFLogInFieldsUsernameAndPassword.rawValue | 
PFLogInFieldsLogInButton.rawValue)

Second way:

You can use operator overloading for shorter code:

func +=(inout slf: PFLogInFields,other: PFLogInFields)-> PFLogInFields{
    slf = PFLogInFields(rawValue: slf.rawValue | other.rawValue)!
}

func +(a: PFLogInFields, b: PFLogInFields)-> PFLogInFields{
    return PFLogInFields(rawValue: a.rawValue | b.rawValue)!
}

And further:

logInViewController.fields = .UsernameAndPassword + .LogInButton

or

logInViewController.fields = .UsernameAndPassword
logInViewController.fields += .LogInButton
like image 35
fnc12 Avatar answered Sep 30 '22 13:09

fnc12


In Swift 2 it seems that the accepted solution or other answers don't work. I solved my problem by enclosing the PFLogInFields in an array. Everything seems to work just fine.

So instead of:

    loginViewController.fields = PFLogInFields.UsernameAndPassword | PFLogInFields.LogInButton | PFLogInFields.PasswordForgotten | PFLogInFields.SignUpButton | PFLogInFields.Facebook | PFLogInFields.Twitter

I wrote:

   loginViewController.fields = [PFLogInFields.UsernameAndPassword, PFLogInFields.LogInButton, PFLogInFields.PasswordForgotten, PFLogInFields.SignUpButton, PFLogInFields.Facebook, PFLogInFields.Twitter]
like image 21
eeschimosu Avatar answered Sep 30 '22 13:09

eeschimosu


Seems this is a moving target, as none of the answers here seem to work anymore. At present I have to use this:

logInViewController.fields =  PFLogInFields.UsernameAndPassword | PFLogInFields.LogInButton
like image 39
jengelsma Avatar answered Sep 30 '22 13:09

jengelsma