Swift中无法识别C指针数组

时间:2016-03-22 21:41:53

标签: objective-c swift legacy pointer-arithmetic

我正在将一些遗留代码转换/导入Swift,我必须使用一个常量文件,如下所示:

Constants.h

extern const int workingConstant;
extern int constantArray1[];
extern int constantArray2[];
extern int constantArray3[];
extern int *problematicConstant[];

Constants.m

const int workingConstant = 3;
int constantArray1 [] = {2,50,50,49,47,46,44,42,16,41,49,47,46,44,42,41,16,64,64,62,62,60,60};
int constantArray2 [] = {72,718,63,740,94,756,117,755,127,759,121,767,120,777,118,788};
int constantArray3 [] = {226,505,226,505,213,518,206,531,230,545,250,562,258,575,265,560,277,543};
int* problematicConstant [] = {constantArray1,constantArray2,constantArray3}

在我的遗留目标C代码中,我可以导入标头并调用类似的方法:

-(void)doStuff:(int)firstConstant paths:(int **)paths shrink_p:(CGAffineTransform *)shrink_p{
   CGMutablePathRef hitPath = CGPathCreateMutable();
    for(int i = 0; i < firstConstant; i++){
        CGPathMoveToPoint(hitPath, &(*shrink_p), paths[firstConstant][i+1]);
    } 
}

采用workingConstant和problematicConstant并且是子视图的方法。我的.swift UIView子类成功找到workingConstant但是当我尝试在子视图上调用doStuff时,problematicConstant会抛出“使用未解析的标识符”错误。

1 个答案:

答案 0 :(得分:1)

我能用一些模拟代码重现你的问题。无论出于何种原因,Swift都无法通过桥接标题看到declare lf varchar2(1) := chr(10); cr varchar2(1) := chr(13); begin execute immediate 'begin'||lf||'null;'||lf||'end;'; execute immediate 'begin'||cr||lf||'null;'||cr||lf||'end;'; execute immediate 'begin'||cr||'null;'||cr||'end;'; end; / 。但是,我能够通过添加另一个全局变量来解决这个问题:

problematicConstant

extern int ** ppInt; // in the header int ** ppInt = problematicConstant; // in the Objective-C implementation 声明和定义可以进入现有的Objective-C源,或者,如果你想保持干净,可以进入单独的头文件和实现文件。实际上,extern只能位于桥接标题中。

顺便说一句,extern声明也无法弥合,但如果你需要它们,你可以做类似的伎俩:

constantArray...