Game Development Reference
In-Depth Information
I'd like to point out two technicalities. First, local variables can be accessed in a block.
But they can't normally be modified, unless they're prefixed with the __block
keyword. Consider this code snippet:
__block bool success = NO;
[localPlayer authenticateWithCompletionHandler:←
^(NSError* error)
{
success = (error == nil);
lastError = error;
}];
With blocks, it's only legal to modify a local variable declared outside the block's
scope if the variable is declared with the __block keyword. In this case, the suc-
cess variable is declared locally outside the block but is modified within the block, so
it must be prefixed with the __block keyword. On the other hand, the lastError
variable is a member variable of the class. You can modify member variables within
blocks without using the __block keyword.
Also, in the case of Game Kit, you'll be frequently passing block objects to Game Kit
methods, but the block objects won't be run until a later time. You're probably used to
code being executed in sequence, but in Game Kit programming it's not! The block
passed to a Game Kit method is called only when the call completes a round-trip to and
from the Game Center server. That takes time because data needs to be transmitted to
the Game Center servers and processed, and then a result needs to be returned to the
device. Only then does the block object get executed.
Let's take an example. You may find yourself tempted to write something like this:
__block bool success = NO;
[localPlayer authenticateWithCompletionHandler:←
^(NSError* error)
{
success = (error == nil);
}];
if (success)
NSLog(@"Local player logged in!");
Search Nedrilad ::




Custom Search