I would like to get started using blocks in my iPhone apps, since they can really be quite handy.
However since all the apps also have to be ported to Android Java, I am not sure if this is a good idea, because I do not have any idea how to easily translate the blocks.
So far porting iOS to Android is straight forward, almost line by line - but how would an iOS block translate to Java ? (of course I am not talking about Java blocks)
Many thanks!
Java allows anonymous (unnamed) classes. What's more, they can extend another class. When used in this fashion, they are similar to blocks in Objective C.
You'll often see an anonymous class defined which implements Runnable or similar. This is doing something similar to blocks: providing an anonymous, in-line definition of code that is passed somewhere for later use.
For more info, see the example here: http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Local_classes_.28Java.29
So in Java you could write something like the following:
doMyThing(new Runnable() {
void run() {
// your 'block' code here
}
});
The method doMyThing could at a later point call the run method (or spawn it as a thread by passing your Runnable into a new Thread object, etc.)
There's further discussion here:
Limitations of Java Anonymous Classes compared to Objective-C Blocks
http://www.jondev.net/articles/Objective-C_blocks_vs_Java_Anonymous_Classes_or_Functions_(callbacks)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With