Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Self-Destructing Message iPhone App Using Parse.com as a Backend and Adding Users Logging In and Logging Out

Passing A Block Error : challenge task 3 of 4

The Question is : Now switch to LoginViewController.m. Blocks can also be declared and passed around as variables, as we can see in the existing code below. This code declares a block named 'loginHandler'. In the login method (after the block), add a call to the PFUser class method 'logInWithUsernameInBackground:password:block:'. Use "test" as both the username and password parameters. Then pass in 'loginHandler' as the third parameter. You can use block variables in this manner like how you would use any other variable as a parameter.

Mycode is :

#import "LoginViewController.h"
#import "UIAlertView.h"
#import <Parse/Parse.h>

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)login:(id)sender {
    // This is an example of how to declare a block named loginHandler. It starts with a
    // return type, then the name of the block with a special syntax for naming. The next
    // set of parentheses hold the parameters and their types. Then assigning the block 
    // on the right side looks just like how we defined it in the video (carat with the
    // same parameters in parentheses).
    void (^loginHandler)(PFUser *user, NSError *error) = ^(PFUser *user, NSError *error) {
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry!"
                                                                message:@"Error logging in."
                                                               delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alertView show];
        }
        else {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
    };

    // Add your code below!

    [PFUser logInWithUsernameInBackground:@"test" password:@"test" block:loginHandler{}];

}

@end

*** Error said : Compilation Error! With the block defined, this should be like a normal method call. Check your syntax and make sure everything matches the instructions.***

**Please someone help me. I've been stucking on this challenge for one hours now. ** Any help will be much appreciated.

3 Answers

Holger Liesegang
Holger Liesegang
50,595 Points

Hi Raja,

try without {} after loginHandler:

[PFUser logInWithUsernameInBackground:@"test" password:@"test" block:loginHandler];

Kind Regards Holger

Holger Liesegang
Holger Liesegang
50,595 Points

You've got to use the block as a variable hence the loginHandler only: Blocks and Variables

Thanks For Your Help Holger. Its's working now. Btw, could u please explain why i need to get rid of {} ?