"阻止指向非功能类型的指针无效"

Wrs*_*ord 15 encoding objective-c objective-c-blocks

这里我有一个编码字符串的方法(它是不完整的),你会发现我的问题是一个错误:"阻止指向非函数类型的指针无效"

+ (NSString *)encodeString: (NSString *)string {
    __block int indexShift;
    __block NSString *dictionary = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    __block NSString *encodeDictionary = @"mahgbcjdfukripylswxovzetqnFMAJWGCQYXLOETPBKSVNIZUHDR";
    __block NSString *encodeString = @"";

    void (^encode) = ^{ // Error here, "Block pointer to non-function type is invalid"
        for (int x = 0; x < string.length; x++) {
            int index = [dictionary indexOf:[string characterAtIndex:x]];
            indexShift += index;
            encodeString = [encodeString stringByAppendingFormat:@"%c", [encodeDictionary characterAtIndex:index+indexShift]];
        }
    };

    return encodeString;
}
Run Code Online (Sandbox Code Playgroud)

请告诉我为什么会发生这种情况,或者我需要更改以解决它.

Mat*_*ing 25

这是用于声明内联块的不正确语法.一般形式如下:

ReturnType(^block_name)(parmeter, types, here) = ^(parameter, types, here) {

};
Run Code Online (Sandbox Code Playgroud)

所以你正在寻找:

void(^encode)() = ^() {

};
Run Code Online (Sandbox Code Playgroud)