2012-08-09 40 views
13

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

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

请告诉我为什么会发生这种情况,或者我需要改变以解决问题。

回答

24

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

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

}; 

所以你要找的:

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

}; 
+0

烨认为这样做是 – Wrsford 2012-08-09 19:47:39

+1

最后一组括号是可选的。如果块类型不带任何参数,你可以直接去'^ {}。 – 2012-08-09 20:40:33