How to create immediately callable functions (blocks) in Swift

I need to create a standalone executable block in Swift, similar to what we had in Objective C:

{
  /* statements */
}

      

However, the same construct in Swift produces a "copied statement block in an unused block".

+3


source to share


1 answer


I am currently using:

if true {
  /* ... */
}

      

Any better solution is appreciated.

UPDATE 2 : Swift 2 now has a new control structure do

:



do {
    /* ... */
}

      

UPDATE . Another answer found here :

func locally(work: () -> ()) {
    work()
}

...

locally {
    /* ... */
}

      

This looks good, except that due to Swift rules, you need to use self.property

instead property

inside a block.

+2


source







All Articles