(void)(^IveBeenBlocked)(NSArray *confusingTopics, NSDictionary *descriptiveAdjectives);

Yesterday I felt pretty good about blocks, today I feel like descriptiveAdjective[@”cussWord”]. Exactly.

Lets say we have a completion handler which requires NSData, NSURLResponse, and NSError to be passed in as arguments. The syntax would look like:
^(NSData *data, NSURLResponse *response, NSError *error) {…}
but note inside the block only the data parameter is used…
<code=”objc”>
{
// inside the block
NSArray *responseArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSMutableArray *issues = [NSMutableArray new];
for (NSInteger i = 0; i < [responseArray count]; i++) {
NSDictionary *firstIssueDictionary = responseArray[i];
Issue *newIssue = [[Issue alloc] init];
newIssue.title = firstIssueDictionary[@”title”];
newIssue.uniqueID = firstIssueDictionary[@”id”];
newIssue.htmlURL = [NSURL URLWithString:firstIssueDictionary[@”html_url”]];
[issues addObject:newIssue];
NSLog(@”Aww Yeah\n%@”, newIssue);
}
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];
}];
</code>
Okay? I feel
Rooster_portrait2images

Lets take a look at this: making blocks into typedefs

We had a project (which was a tad more intuitive) where the goal was to remake the delegate methods of UIAlertView into blocks. A bit more understandable, because if you see here:

– (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

– (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

– (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex

they all do different things, but they share the same return type and arguments. So we can place them all into a typedef block:

typedef void (^TheBlock)(UIAlertView *alertView, NSInteger index);

Apple wants us to create blocks like this:

Whuuut? Why bother with that when we have functions that can do the same thing?

int multiplier = 7;

– (int) myMethod:(int)num andMultiplier:(int)multiplier

{

        return num * multiplier;

}

Doesn’t that look easier?

tumblr_mf8xtra83Y1ql2603o1_400

Stop it Apple. STOP IT!

Saw a snippet of Lynda.com and that helped a bit. More like a tad.

basic block:

return type       name       param     block literal

      int            (^myBlock) (int) = ^(int a) {….};

According to Lynda, we can drop the return type, name, and parameter at the left of the equals sign and just keep the literal. So this is all:

^(int a) {….};

looks better, feels better.

While watching iTunes U, they used blocks only for Multithreading. Shown here:

Screen Shot 2014-07-15 at 9.37.00 PM

and here:

Screen Shot 2014-07-15 at 9.35.41 PM

So here the blocks are only handling URL sessions. Which makes sense, if we want to wait for a website to load, the user could believe that our app crashed. So our app gets a low rating. Blocks allow us to do something else while whatever data we need is still loading behind the scenes, on another thread. The only saving grace of blocks is here.

Apple, don’t do it.

Totally+cock-blocked+Even+if+you+_98b85061b0d78da0c318a3f2cfe00a87

**Disclaimer WordPress is awful- I had to type this out a second time losing heaps of content because for some ridiculous reason at 9:40pm this website did not properly save my first draft and posted half of the blog. Yeah you heard me WordPress. Fix It!

Advertisements
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s