Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Objective-C Blocks Syntax

Avatar for Nils Hayat Nils Hayat
November 21, 2013

Objective-C Blocks Syntax

Presentation version of my blog post about how the blocks syntax came to be in Objective-C that I made for CocoaKucha NYC on November 20th 2013.

You can find the original blog post at http://nilsou.com/blog/2013/08/21/objective-c-blocks-syntax/

Avatar for Nils Hayat

Nils Hayat

November 21, 2013

Other Decks in Programming

Transcript

  1. Objective-C Blocks • Introduced in OS X 10.6 / iOS

    4.0 • Autonomous piece of code • Capture their environment
  2. Reading rules • Start from the identifier • Go right

    until you can’t • Then read to the left of the variable
  3. Modifiers • Pointer Modifier: * • Array Modifier: [] •

    Function Modifier: () • Block Modifier: ^
  4. Pointers • a is a pointer to an int. •

    Immediately to the left of the modified variable
  5. Array • a is an array of ints. • Always

    to the right of the array. • Can be completed by the dimension: int a[10];
  6. Function • f is a function that returns an int

    • can be completed by the type of the arguments • Always to the right of the variable
  7. Precedence • Similar to * / + - • []

    and () have higher precedence than * and ^ • This explains rule right then left • You can compose multiple modifiers
  8. Pointers and Arrays ! • a is an array of

    pointers to ints. ! • What if you want a pointer to an array of ints?
  9. Arrays and Functions • You cannot compose [] and ()

    • No array of functions • No function that returns arrays ! • f is a function that takes an array as an argument
  10. Pointers and Functions ! • f is an function that

    returns a pointer to an int ! • What if you want a pointer to a function?
  11. The block pointer modifier • Similar to function pointers •

    ^ (caret) can only be applied to a function • the ^ symbol and the identifier ALWAYS NEED TO BE BETWEEN PARENTHESIS
  12. Abstract Declarators • Declarator has 2 roles (give the type

    of variable & name it) • abstract declarator has only one role : give the type • just remove the identifier from a declarator!
  13. Abstract Declarators • Used in 3 places in standard C

    • In casts: int *a; long *b = (long *) a; • in sizeof(): malloc(sizeof(long *)); • When declaring arguments type for a function: int f(long *);
  14. Abstract Declarator • Objective-C uses them in one more place

    • For methods’ return values or argument types - (long **)methodWithArgument:(int *)a;