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 trialAlex Cevallos
16,551 PointsDifficulty understanding iOS Blocks, help
hello I have resolved the issue, thanks
2 Answers
Patrick Serrano
13,834 PointsThey are passing the block as the method parameter. I'll try to break it down for you since blocks can be a little confusing:
In the example you posted you are calling findObjectsInBackgroundWithBlock:
on the query
object.
The findObjectsInBackgroundWithBlock:
method takes a block as a parameter.
The block you are passing into findObjectsInBackgroundWithBlock:
has two parameters (NSArray *objects, NSError *error)
Inside the block you are writing an if
statement. (the if
statement is being used to check for an error before executing the code that you're trying to execute with the block.)
There are a few different places and ways to call block and they have slightly different syntaxes. You can take a look at Fucking Block Syntax for examples of the most common syntaxes.
Alex Cevallos
16,551 Pointsaccidental comment
Patrick Serrano
13,834 PointsI can't reply to your reply so I'll do it here, but the site I linked to just shows the syntax for blocks generically. It isn't for a specific method or anything like that, but instead meant to be a place to look to when you're looking to write your own block. I'm not 100% sure what you mean when you say "Does that mean you just attach the arguments to the method?"
The findObjectsInBackgroundWithBlock:
method returns an NSArray, which is then passed into the block with the parameter name objects
for you to use. In the video for this part of the course you see in the else
block, the property self.allUsers
is set equal to objects
.
Alex Cevallos
16,551 PointsAlex Cevallos
16,551 PointsHey thank you for the help! I looked at that site you sent and couldn't find the syntax for findObjectsInBackgroundWithBlock ? The documentation says link The block should have the following argument signature:(NSArray objects, NSError error). Does that mean you just attach the arguments to the method?
thanks again