Creating the Demo Appīefore we make any discussion and see any single line of code, it’s necessary to have a project ready to be waiting for us. The most important is that each part of our app will focus on another way of using blocks, so useful techniques to be demonstrated in practice. However, we’ll do some interface building as well, as we are going to perform some view-related tasks and we want our application to provide UI interaction. That’s because we’ll focus on making our debut in writing blocks, and that part will consist of multiple, small examples, for which we care about only for the returned results.
BLOCKSWORLD HOW TO MAKE BLOCKS DISAPPEAR CODE
First of all, we are going to create some code that will display messages only on the debugger, without build any special UI for it. Actually, we could implement a command-line application, but as I am going to present some UI-related content, we’ll make a really simple application. I could say that the demo application we’ll create for the purposes of this tutorial is composed by multiple parts that are independent on each other. So, keep reading in order to discover some cool and interesting stuff! App Overview If you don’t do any block programming yet, I wish after having finished this tutorial to start doing so. In this tutorial I’m aiming at two goals: To clarify all these things I just presented using code, and, above all to show you how blocks can be used in practice, by demonstrating practical examples, so they can make your programming life easier. If you are new to all these, don’t worry, as after a couple of days of using and writing blocks, you will get used to it like everything else in Objective-C. Secondarily, thankfully or not, you don’t need to have deep knowledge on block programming in order to use them, just to understand how they work.īlocks have just one weird characteristic, and that’s the way they are written. That’s because Apple has adopted blocks especially as completion handlers to many important, commonly used methods. First off, it’s certain that every one has used blocks, even without knowing it. Two facts are sure for every developer, even if someone has no idea about blocks. The best practice is to make combination of both, and that can be achieved both by following some simple rules and using the experience obtained in time.īlocks are objects, so they can be stored to NSArray or NSDictionary data structures, as well as to be returned from methods, even to be assigned to variables. However, blocks cannot totally replace delegates as they cannot be used for every purpose.
The best thing though, is that the callback procedure can directly access and use any variables existing locally in the scope where the block has been defined, so the need of passing data like we do in callback methods is eliminated. Using blocks, there is no need to adopt protocols, or implementing delegate methods that lead to much more code in a class. Their usage leads eventually to a much cleaner and tidier code writing, as they can be used instead of delegate methods, written just in one place and not spread to many files.įocusing even more on the second feature, blocks offer a nice solution for creating callbacks instead of delegates, as they are declared and implemented directly to the point of the code where they are invoked.They can be executed in a later time, and not when the code of the scope they have been implemented is being executed.Also, blocks can modify the values of variables being out of its body using a special way (we’ll see more later).
The code on the block can “interact” with the world out of it, but what takes place in the block is not visible to the scope out of it. Well, a block is a self-contained, autonomous code fragment, existing always into the scope of another programming structure, as for example the body of a method. In subsequent iOS versions, Apple re-wrote or updated many framework methods so they adopt blocks, and it seems that blocks are going to be partly the future of the way code is written. In iOS, they first-appeared in version 4.0, and since then they’ve known great acceptance and usage. They exist in other programming languages too (such as Javascript) with other names, such as Closures. One of them, is the capability to write code using Blocks.īlocks do not consist of a new programming discovery in Objective-C.
New developers have a lot to explore when starting working with it, while advanced programmers have always something new to learn, as there are numerous supported programming aspects.
BLOCKSWORLD HOW TO MAKE BLOCKS DISAPPEAR MAC
Objective-C, the official language for developing applications for iPhone, iPad and Mac OS, is a multi-featured one, and as a relative to C, very powerful. In programming, what differentiates a good developer from a great developer is the way each one takes advantage of the programming tools he or she offered by the used language.