Swift: Could not find overload for '|' which accepts the attached arguments

An attempt to incorporate Parse into a new Swift project.

When I get to this block:

logInViewController.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsLogInButton | PFLogInFieldsSignUpButton | PFLogInFieldsPasswordForgotten 

I get this error in Xcode 6:

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

Does anyone know what is wrong with this syntax?

+7
ios swift
source share
4 answers

Use .value , then use the result to instantiate the PFLogInFields :

 logInViewController.fields = PFLogInFields(PFLogInFieldsUsernameAndPassword.value | PFLogInFieldsLogInButton.value) 
+8
source share

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

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

The 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 
+2
source share

In Swift 2, it seems that the decision made or other answers do not work. I solved my problem by including PFLogInFields in an array. Everything seems to be working 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] 
+2
source share

This seems to be a moving target, since none of the answers here seem to work anymore. Currently I have to use this:

 logInViewController.fields = PFLogInFields.UsernameAndPassword | PFLogInFields.LogInButton 
+1
source share

All Articles