Les recomiendo que lean:
- The Pragmatic Studio: Using Blocks in iOS 4: The Basics
- The Pragmatic Studio: Using Blocks in iOS 4: Designing with Blocks (ver también al final del artículo que tiene otros links interesantes)
- Programming with C Blocks (On Apple Devices)
- iOS Reference Library: A Short Practical Guide to Blocks
^ (int num) {Este block se puede asignar a una variable, por ejemplo con
return num * 3;
}
(int)(^por3)(int) = ^ (int num) {return num * 3;};y luego invocarlo con:
int result = por3(7); // result = 21o se puede pasar como parámetro a algún método que reciba un block con el tipo dado:
[someObject someMethodWithBlock:^ (int num) {return num * 3;}];Los blocks no están tan integrados al lenguaje como lo están por ejemplo en Ruby, pero es bueno ver que en iOS 4 Apple agregó a varias clases la posibilidad de pasarles blocks cuando resulta útil. Citando lo que dice la documentación oficial:
One obvious motivation for using blocks is that an increasing number of the methods and functions of the system frameworks take blocks as parameters. One can discern a half-dozen or so use cases for blocks in framework methods:
- Completion handlers
- Notification handlers
- Error handlers
- Enumeration
- View animation and transitions
- Sorting
No hay comentarios.:
Publicar un comentario