Discussion in 'iOS Programming' started by Will0827, Jun 25, 2014.

  1. Will0827 macrumors regular


    Aug 11, 2008
    Since i started learning objective c one thing i never got the grasp of is blocks and now closures. I feel like after watching and reading the recent swift vids and guide provided by apple im starting to understand a little better, i just wanted to see if i have the write idea. From what ive been able to gather, it is just random chucnks of code that can be passed or return and used as parameters. And to allow for cleaner code when using them in a class file. I was confused because i asked why not just write whatever the method and its definition in the .h and .m files or just in the single file in swift. Can you essentially have a random file with a bunch of blocks and import that file to whatever class and use a specific block ?. Thats kind of the idea i have about blocks/closures wasnt exactly sure of the use case of them, seeing as they are written similar to functions, writting them and the shorthand syntax that now exist in swift isnt as confusing but the use case and purpose had me for a loop. Thanks mr family.
  2. 1458279 Suspended


    May 1, 2010
    I don't think I can explain any better that books, but as I understand it, your thinking is not correct.

    "Blocks are Objective-C’s anonymous functions" -RyPress

    It's basically a pointer to a function/method.

    Example of use: (someone correct me where I'm wrong)

    You can define 10 blocks to process things differently, then you can have one routine, pass the block you want to that routine for further processing.

    The routine that gets the block as a parameter, doesn't have to know what it does, it just processes it.

    It's a bit more advanced topic and I don't think you have to use it for most programming. I think it's more of a thing to use if you find an advantage to it.
  3. D.T. macrumors G3


    Sep 15, 2011
    Vilano Beach, FL
    There’s a functional value, but it’s also a matter of design, readability, encapsulation, etc.

    The main thing to remember is a closure, or block, maintains the scope of the original inclosing function, even after the original function has been purged.

    I saw a definition quite a while back I kind of liked:

    "An object is data with functions, a closure is a function with data.”

    So in other words, the anonymous function gets initialized with some values from the enclosing function, the enclosing function loses scope, but the closure maintains the values wrapped up into a defined function, that can passed around like a variable to other functions.

    As an example:

    In our app, during a UI event, we take input, setup values for various state changes, and wrap that into a block. That block is passed to a general backend handler (using AFNetworking). If the service call is successful, we execute the block which updates the UI (counter, icon state, etc.)

    This way, the event handler passes in the logic of how to handle the UI, the networking library is very clean, and doesn’t need references to UI elements, the networking is more generic and the different events which handle different UI elements contain the logic relevant to their function.
  4. ArtOfWarfare macrumors G3


    Nov 26, 2007
    I come from an Engineering background, not a CS background, but I have taken a few CS courses and it seems to me that they make lambda/blocks/closures/anonymous functions all more complicated seeming than they need to be.

    First off, those are all referring to the same exact thing.

    They allow you to pass code as an argument to a function or method that tells it what to do. Use cases would be times like NSArray's enumerateWithBlock - you want to use that method and pass code to it which tells it what to do with each item in the array.

    You could also use it as a completion handler. Maybe you want to write a function which uploads something to the web, and then runs some code which the function's user provides when it's done. You don't know what code should run - that's up for the user of your function to decide - you just make your function accept the block and execute it at the right time for them.

    This serves the same purpose as passing around function pointers in C. So why do these exist instead of using the C solution? Blocks allow you to define the code right where it'll be needed/used. Your function will only ever be needed right where it's passed in. It helps organize your code and keep related code together.
  5. subsonix macrumors 68040

    Feb 2, 2008
    The difference with function pointers is that data from the outside needs to be passed in as an extra parameter, if there is more than one item then it's often a struct containing the data.
  6. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    It's mainly a naming difference.

    With C function callbacks, one normally has to copy some state data into a callback data struct, pass this data block with the function pointer to the callback, then inside the callback function refer to the data one copied in by different local names or pointer dereferences from the struct.

    With blocks or closures, the language compiler and/or runtime figures out what data you need and copies the data into a struct for you, passes the struct of data to the callback for you, and lets your function use the exact same variable names for the data both before and inside the callback.

    So you don't have to manually deal with callback data structs, and remember a bunch of potentially different names for the same variables and data you want to use.

Share This Page