Search code examples
ioscompilationblockarm64

ios custom block can not compile for arm64


When change to arm64 from armv7s, I can't compile the custom block. How I can reslove the problem? Thanks. Here is my code:

//typedef NSInteger (^ActionSheetPickerNumberOfRowsBlock)

(ActionSheetPicker *picker);
    ActionSheetPickerNumberOfRowsBlock numberOfRows = ^(ActionSheetPicker *picker) {
        return 29;
    };


Solution

  • 29 defaults to type int, so the compiler infers return type int for the block, instead of NSInteger as needed. NSInteger is the same as int in armv7, but not arm64.

    You can see from this table:

    • int is 32-bit in both armv7 and arm64
    • NSInteger is 32-bit in armv7 and 64-bit in arm64
    • long is 32-bit in armv7 and 64-bit in arm64 (same as NSInteger)

    So you can either do:

    return 29l;
    

    or

    return (NSInteger)29;
    

    or explicitly specify the return type in the block literal:

    ^NSInteger(ActionSheetPicker *picker) {
        return 29;
    };