Philipp Jahoda Philipp Jahoda - 4 months ago 8
iOS Question

How to write this objective-c block in swift syntax?

I have the following objective-c block.
I cannot figure out how to write it in swift syntax.
This is how the block is defined in objective-c:

formatter = ^(CGFloat value){

// do something with value

return "stringrepresentation";
};


Could someone be kind and show me the correct syntax?

ADDITIONAL Info: the "formatter" value needs to be of type
ValueFormatter


How to achieve this?

Answer

Look at closures.

{(/*Parameters*/) -> /*ReturnType*/ in
    // Statements
}

So this block would be:

{(value : CGFloat) -> String in
    // Do cool stuff
    retun "aString"
}
Comments