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

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

Code challenge Logging in and Logging out 3 of 4

I am stuck on this one.

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.

Here is my code:

// Add your code below!
[PFUser logInWithUsernameInBackground:test password:test block:loginHandler];

}

@end
Spencer Fornaciari
Spencer Fornaciari
7,780 Points

I'm having trouble on this one. How do you pass a block as a block without paramaters?

I tried block:loginHandler {} I tried block:^loginHandler {} I tried block:^(loginHandler) {} I tried block:^(^loginHandler) {}

When we used it in the example it was block:^(PFUser *user, NSError *error) {} so we were passing it parameters not a block itself. Any suggestions on how to proceed forward?

Spencer Fornaciari
Spencer Fornaciari
7,780 Points

Here is the full code of what I'm talking about: [PFUser logInWithUsernameInBackground:test password:test block:^loginHandler {

}];

Any tips on how to correct this? I'm given the error: "Compilation Error! With the block defined, this should be like a normal method call. Check your syntax and make sure everything matches the instructions."

4 Answers

Christer Nordbø
Christer Nordbø
2,133 Points

Take a look at your username/password parameter.

Thomas Skjelstad
Thomas Skjelstad
11,541 Points

I got past this one yesterday, I am not good with blocks apparently.

This is where i am stuck now. I am trying to do just what the Challenge is telling me to do.

Now send the 'findObjectsInBackgroundWithBlock' message to the 'query' variable. Use the code that is commented out as the basics for the block you pass in.

Here is my code.

#import "AppsViewController.h"
#import <Parse/Parse.h>

@implementation AppsViewController

- (void)viewDidLoad {
[super viewDidLoad];
PFQuery *query = [PFQuery queryWithClassName:@"Apps"];


[PFQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
    else {

    }
}

}

@end

Do you see what i am doing wrong?

hi guys im still on 2/4 would anyone like to give a hint or an answer with a reason

Christer Nordbø
Christer Nordbø
2,133 Points

Hi Elaine, im sorry, but your question is unclear. 2/4 of what? And you can post it as a new Topic :) with a full problem description please.

:)

hey guys. so i passed 2/4 but im afraid I'm still not getting the answer for 3/4. even after reading this i still dont have it. so ya need help for 3/4.

Spencer Fornaciari
Spencer Fornaciari
7,780 Points

Yeah, I'm with you, I don't think I understand how to pass loginHandler to a block, and advice on that would be appreciated.

Spencer Fornaciari
Spencer Fornaciari
7,780 Points

Ben Jakuben

I'm stuck on this one still. I'm not entirely sure how to pass the block, this is what my code currently looks like:

[PFUser logInWithUsernameInBackground:@"test" password:@"test" block:loginHandler(PFUser *user, NSError *error) {
    }];

I've also tried several other variations: [PFUser logInWithUsernameInBackground:@"test" password:@"test" block:loginHandler { }];

And

[PFUser logInWithUsernameInBackground:@"test" password:@"test" block:^loginHandler(PFUser *user, NSError *error) {
    }];

No dice. Any tips would be appreciated.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

So for this one, this part of the instructions is quite literal: Then pass in 'loginHandler' as the third parameter.

Because the block is already defined, you don't need any other characters or syntax to pass the block as a parameter. You can reference it by name like any other variable.

So, I'm currently stuck on this, too.

My code is:

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

and I thought that this was how you pass in an object as a parameter, so I'm confused what I'm doing wrong.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

That looks okay - can you paste in the rest of your code from the Code Challenge?