Welcome to my Programming Blog...

I am currently working on a couple of projects: An original game called "Implosion" which I am porting from Flash to the iPad, and a remake of Q*bert in Python (pygame), as part of the class I am teaching. Please feel free to use the "Labels" (at right) to follow a specific project or theme. If you are one of my Python students, I recommend that you check out the Python thread.

Saturday, October 16, 2010

#VTM_iPhone notes on around the block with blocks talk

Here are my notes on Bob Clair's "around the block with blocks" talk

I feel kind of foolish about this - I hadn't heard about blocks until today, so I'm glad I'm here for this presentation.

He says there are some typos in original presentation posted , but new one is forthcoming.

Blocks are not in ios3.2 ( which would explain why I haven't heard of them.)

What problem are we trying to solve. How to package up some functionality (some code to be executed) to pass on to somebody else.

In other languages, called "closures" or "anonymous functions"

Blocks are an apple addition to C, objectiveC, c++

New in snowleopard and ios4

Diversion 1: functions calls are jumps, which you can make dynamic by using a pointer to a function.

(ugly) Example: Void (*myFunctionPtr)(int)

See PowerPoint for code with example. Too much to type!

Main use of a function pointers is for callbacks. (hooks? -hh)

This stuff works, but it's a pain in the butt, particularly when passing lots of data.

You can also do this with selectors SEL objects.

Diversion#2: nsinvocation
Bundles up (freeze dries) receiver, method and arguments. Then the invocation can be interrogated to find the return value.

More ugly code as example in PP...

The issue is that these things are a royal pain to set up.


Blocks:
^(arg list) {body};

No return type declared. This is a block literal.

There is no name for this it is anonymous.
A trivial example.
^(int n) {return n*2;};

How do you call this?

Int j = ^(int n) {return n*2;}(9);

But this is dopey. Send it to a block variable.

Declare a variable that is a pointer to a block

Return type (^myBlock. Afasdhjohfoafhgkj

Int (^doubler)(int);
Doubler = ^(int){return n*2;};

Too fast to keep up. See pp.

Funny syntax when trying to pass a block as a function argument.

So what is the big deal?!?

A block has read only access to automatic variables visible in it's enclosing scope! <-------

When you declare the block, it freezes the value of any extra-scope variables it references in the block. The variable is copied into a structure with all the ?

The block is created on the stack, not the heap. Blocks have same lifetime as an automatic variable (local variable?) when the block literal goes out of scope, it is undefined.

A block variable is declared with type edifier __block

__block int myBlockInt

Can be read/write by a block in the near by scope.

Under the hood.
Blocks are actually objectivec objects

Only example of stack based objectivec
A private subclass of nsobject
The header isn't provided so you can't subclass them or monkey around with them.

Passing a block into a function as an argument is safe, in a single thread.
What if you want to pass the block to another thread or pass it back out of a function to use later?
To do this, you must... Copy the block. He mentioned some c methods, but I'm just listing objc techniques here.
In objc, use the [. Copy] method on the block, since it descends from nsobject. The copied block goes to the heap, and moves any __block variables and fixes any links to those variables. ( mostly works)

Memory management in objc, remember doing copy requires a release or autorelease.

Some uses for blocks: collection classes, animation, threading, completion handlers

Also useful in grand central dispatch - give GCD blocks to run for you.

Gripes: 1) block is a word already in use 2) smashes encapsulation 3) function oriented, not object oriented 4) encourages Design Your Own Language Syndrome

Plug the book "Learning Objective-C 2.0" by Robert Clair. This is chapter 16.


- Posted using BlogPress from my iPad

No comments:

Post a Comment