1

I am having trouble trying to change Objective - C code to Swift. This is with the Parse framework. If anyone knows how the following code should be written in Swift, it would help me a lot.

 [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (!error) {
        //The registration was successful, go to the wall
        [self performSegueWithIdentifier:@"SignupSuccesful" sender:self];

    }

3 Answers 3

6

[NSObject: Anyobject]? does not have a member named subscript parse compile error will be thrown.

The code should be like that because userInfor maybe be nil.

user.signUpInBackgroundWithBlock {
    (succeeded: Bool!, error: NSError!) -> Void in
    if !(error != nil) {
        // Hooray! Let them use the app now.
    } else {
        if let errorString = error.userInfo?["error"] as? NSString {
            println(errorString)
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can even improve your solution casting like that: if let errorString = error.userInfo?["error"] as? String {
1

Parse happens to have an example of this exact method in their documentation. I think they'll be OK if I excerpt it here:

user.signUpInBackgroundWithBlock {
  (succeeded: Bool!, error: NSError!) -> Void in
  if !error {
    // Hooray! Let them use the app now.
  } else {
    let errorString = error.userInfo["error"] as NSString
    // Show the errorString somewhere and let the user try again.
  }
}

1 Comment

This solution doesn't work using Xcode 6.1 and iOS 8.1. See rather the Meshari Alsuhaibani's solution below.
0

It would be something like this.

user.signUpInBackground {
    (success: Bool!, error: NSError!) -> Void in
    if !error {
        [unowned self] in
        self.performSegue("SignupSuccesful", sender:self);
}

Edit: Parse actually has Swift support. Here's a tutorial for it.

Edit2: You shouldn't refer to self inside a block. Use unowned self instead.

3 Comments

You can bridge their current Objective - C framework to Swift. So their framework works with Swift.
This isn't valid Swift. Type annotations go after variable names, and you don't signify pointers with *.
Sorry, I edited the user's code and forgot to edit that part.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.