GoodluckH
GoodluckH

Reputation: 135

Changes in Swift 1.2 that confusing me

I updated Xcode to 6.3, and found there’s some new error in my codes with new Swift 1.2.

user.signUpInBackgroundWithBlock {
    (success:Bool!, error:NSError!) -> Void in

    if !(error != nil) {
        println("sign up successfully")

        var loginAlert: UIAlertController = UIAlertController(title: "Sign Up", message: "Sign Up Succeeded", preferredStyle: UIAlertControllerStyle.Alert)
        self.presentViewController(loginAlert, animated: true, completion: nil)
        loginAlert.addAction(UIAlertAction(title: "Okay", style:

I got this error:

Cannot invoke signUpInBackgroundWithBlock with an argument list of type ((Bool!, NSError!) -> void)

How can I fix it?

Another one

@IBAction func endend(sender: AnyObject) {        
    if (PFUser.currentUser() == nil) {
        PFUser.logInWithUsernameInBackground(usernameTextField.text, password: passwordTextField.text){  
            (user:PFUser!, error:NSError!) -> Void in

            if user != nil {
                println("login chenggong")
                var tlvc = TimelineViewControllerTableViewController()                
                self.presentViewController(tlvc, animated: true, completion: nil)
            }
            else {
                println("failed")
            }        
        }    
    }
}

I got this error :

“UITextField” does not have member named “text”.

And I got 3 errors that about }, it says

Expected “,” separator.

Expected expression in list of expressions.

Expected “)” in expressions.

I can ran my app before Swift 1.2, but now...

Upvotes: 1

Views: 1422

Answers (4)

Tarun Prasad
Tarun Prasad

Reputation: 11

Following code worked for me:

PFUser.logInWithUsernameInBackground(username.text as String!, password: password.text as String!){
                (loggedInuser: PFUser?, signupError: NSError?) -> Void in

Upvotes: 1

user916915
user916915

Reputation: 33

Try changing:

 user.signUpInBackgroundWithBlock{(success:Bool!, error:NSError!) -> Void in

To:

 user.signUpInBackgroundWithBlock{(success:Bool?, error:NSError?) -> Void in

Upvotes: 0

Mark Barrasso
Mark Barrasso

Reputation: 649

For Parse sign up blocks, the Swift 1.2 compiler does not like it when you force unwrap the boolean success parameter.

Removing the '!' after 'success:Bool' should remove the errors you are getting.

Try changing:

 user.signUpInBackgroundWithBlock{(success:Bool!, error:NSError!) -> Void in

To:

 user.signUpInBackgroundWithBlock{(success:Bool, error:NSError!) -> Void in

Upvotes: 0

egracer
egracer

Reputation: 362

In Xcode, go to Edit > Convert... > To Latest Swift Syntax...

There are several language syntax changes in the new release, so Apple included a tool to help migrate older Swift code. From what I've been reading it's largely useful but doesn't always solve 100% of the problems. Hopefully it'll at least reduce the number of errors you're seeing.

Upvotes: 0

Related Questions