Objective-C Block

objc_block

In Objective-C, you can pass a primitive type variable, a pointer or a object to a function. You can also pass a piece of code, which can be regarded as a “function”, to a function. This is called block in Objective-C. Block looks complicated at first, however, once you start to analyze the syntax, you will find out that the syntax is really intuitive and straightforward.

Here are two great posts that I want to recommend to you.

Also, here is a great sample code from GitHub

Since I am really familiar with C, I also want to summarize the block syntax by talking about C syntax first.

Objective-C Block Syntax and C

I believe that one of the best ways to learn new ticks is to relate those new ones to some old ones that I have already knew. I am familiar with C, so I’ll start with C.

There are four modifiers that are extensively used in Objective-C block: ^, *, [], {}. Fortunately, only the first one is not included in C. The others are really common modifiers in C. If you are a C person, you will find them familiar.

It is important to keep how to read a declaration in mind. The first thing is to find the variable name or identifier. Then, your should go right until you can’t and the start over from the left of the variable. A declaration can only have 1 basic type and it’s the left most word of the declarator. Another important thing is that [] and () have higher precedence over * and ^.

Here is a list of C declarators

1. Basic declarator.

a is the variable name, and int is the type.

2. The pointer modifier

3. The array modifier

4. The function identifier.

The function name is a, and the return type is int.

5. Array of pointers.

Since [] has higher precedence than *, a is an array first. Then find the basic type which is the pointer to int. Thus, a is an array of points to int.

6. Pointer to array.

Again, because of the precedence rule, if you want a pointer to array, you need a pair of parenthesizes like this.

7. Function takes array as parameter

You can’t have an array of function and a function cannot return an array of a function. However, A function can take an array as argument.

8. Function returns pointer

As () has higher precedence than *, f is a function first and returns an int.

9. Pointer to a function

Similar to the relationship between array and pointer, if you want a pointer to a function, you need to use parenthesizes.

The Block Pointer ^

The block pointer ^ is similar to a pointer modifier. Since the block is similar to a function, you declare a block the same way you would declare a pointer to a function. Just replace the * in int (*f)() with the block pointer ^.

b is the block pointer to a function that returns an int.

A block can also have parameters.

Abstract Declarator

Declarator = Abstract Declarator + Identifier

The abstract declarator for the block above will be int (^)(long a, long b) or int (^)(long, long)

Examples:

How to Use Blocks

As a local variable

Template:

Example:

Here is an example which uses block to calculate addition. You will the output of 300.

As a property

As a method parameter

As an argument to a method call

Template:

Example:

As a typedef

Template:

Example:

18 thoughts on “Objective-C Block

  1. Hello. And Bye.

  2. Thanks for your information on this blog. A single thing I would wish to say is the fact that purchasing gadgets items through the Internet is not new. The fact is, in the past decade alone, the market for online electronic products has grown significantly. Today, you could find practically just about any electronic unit and gizmo on the Internet, ranging from cameras along with camcorders to computer components and video games consoles.

  3. Superb, what a webpage it is! This weblog gives helpful data to
    us, keep it up.

    Feel free to surf to my page – Alberton Fix Leak In Copper Pipe

  4. Do you mind іf Ӏ quote a couple of your articlеs as loong as I proviԁe credit ɑnd sߋurces back
    to your websitе? My blog is in the very same nichhe as ykurѕ and my visіtors wоuld truly bеnefit frߋm some of thhe information you present here.

    Please let me know if this alriǥht withɦ you.
    Thank you!

    Also visit my blog post :: nettikasinot

  5. I just like the helpful info you provide to your articles. I will bookmark your blog and test again right here regularly. I’m moderately certain I will be informed many new stuff proper here! Best of luck for the following!

  6. Well I truly liked reading it. This tip procured by you is very practical for good planning.

    • I have been exploring for a little bit for any high quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this web site. Reading this inromfation So i’m happy to convey that I’ve an incredibly good uncanny feeling I discovered exactly what I needed. I most certainly will make certain to do not forget this web site and give it a glance regularly.

  7. you are truly a good webmaster. The site loading velocity is incredible. It sort of feels that you are doing any unique trick. Moreover, The contents are masterwork. you’ve performed a fantastic task on this matter!

  8. I have read several good stuff here. Definitely worth bookmarking for revisiting. I wonder how much effort you put to create such a wonderful informative web site.

  9. I needed to thank you for this wonderful read!!
    I definitely enjoyed every little bit of it. I have you saved as a favorite to check out new things you
    post…

  10. I have read so many posts on the topic of the blogger
    lovers except this piece of writing is truly a nice article,
    keep it up.

  11. We are a group of volunteers and opening a new scheme in our community.
    Your web site offered us with valuable information to work on. You have done a formidable job
    and our entire community will be grateful to you.

  12. Wonderful post but I was wanting to know if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Thank you!

  13. Just wish to say your article is as amazing. The clarity on your publish is simply cool and that i
    could assume you’re knowledgeable on this subject. Well together with your permission let me to clutch your RSS feed
    to stay up to date with impending post. Thank you one million and please keep up the gratifying work.

Leave a Reply

Your email address will not be published. Required fields are marked *