Search code examples
objective-cblock

Incompatible block pointer type initializing float


Im not sure how to get this to work: I was thinking something to do with typedef, but I can't find much on the subject.

    float (^pixelsToDistance)(float, float, NSString *) = ^(float distance, float scale, NSString *conversion)
    { 
        // Code goes here        
    }

Im trying to return a float value from this block function. Should I use a function instead?


Solution

  • You were close. You want:

    float (^pixelsToDistance)(float, float, NSString *) = ^float(float distance, float scale, NSString *conversion) { ... };
    

    Note the return type after the ^ on the right hand side of the assignment operator.

    As the commentator points out, you can omit the return type if it's clear to the compiler what you're returning from the block. E.g.:

    float (^pixelsToDistance)(float, float, NSString *) = ^(float distance, float scale, NSString *conversion) { return 0.0f };