我正在将一些旧代码转换/导入到 Swift 中,并且必须使用如下所示的常量文件:
常量.h
extern const int workingConstant;
extern int constantArray1[];
extern int constantArray2[];
extern int constantArray3[];
extern int *problematicConstant[];
常量.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}
在我的旧版 Objective C 代码中,我可以导入 header 并调用如下方法:
-(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]);
}
}
它采用工作常量和问题常量,并且是 subview 的方法。我的 .swift UIView 子类成功找到工作常量,但是当我尝试在 subview 上调用 doStuff 时,有问题的常量抛出“使用未解析的标识符”错误。
最佳答案
我能够使用一些模拟代码重现您的问题。无论出于何种原因,Swift 都无法通过桥接头看到problematicConstant
。但是,我可以通过添加另一个全局变量来规避这个问题:
extern int ** ppInt; // in the header
int ** ppInt = problematicConstant; // in the Objective-C implementation
extern
声明和定义可以放入现有的 Objective-C 源代码中,或者,如果您想保持干净,可以放入单独的头文件和实现文件中。事实上,extern
可以只存在于桥接 header 中。
顺便说一句,constantArray...
声明也无法桥接,但如果您需要它们,您可以执行类似的技巧:
int * pInt1 = constantArray1;
...
关于objective-c - Swift 无法识别 C 指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36165792/