objective-c - "Block pointer to non-function type is invalid"

标签 objective-c encoding objective-c-blocks

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

+ (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;
}

请告诉我为什么会这样,或者我需要更改什么来修复它。

最佳答案

声明内联 block 的语法不正确。一般形式如下:

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

};

所以你要找的是:

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

};

关于objective-c - "Block pointer to non-function type is invalid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11890631/

相关文章:

ios - 双@synchronized : is it needed?

objective-c - 在没有子类化的情况下获取 NSSearchField 上的功能键

objective-c - NSAttributedString kCTParagraphStyleSpecifierParagraphSpacing 无效

cocoa - iOS5 : UTF-8 encoding does not work at all

objective-c - 将 Objective-C block 转换为 Swift 闭包

objective-c - 在 Xcode 中使用 Cocoa Framework 显示窗口

python - 如何使用 python 将图片字节包含到 JSON? (编码问题)

php - 带重音的记录上带有 "b"前缀的字符串

ios - 为什么是 NSEnumerationReverse 而不是 NSEnumerationForward

objective-c - 在 Objective-C block 的实现中调用 super