objective-c - Swift 无法识别 C 指针数组

标签 objective-c swift legacy pointer-arithmetic

我正在将一些旧代码转换/导入到 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/

相关文章:

python - 为什么我的 python 和 objective-c 代码得到不同的 hmac-sha1 结果?

ios - #warning 此方法不再使用(Objective-C)

swift - 访问 iCloud 日历事件 - macOS 应用程序

ios - 无需在 View Controller 中使用 loadFromNib 即可快速使用 Nib 自定义 UIView

ios - SwiftUI 将数据从 View 传递到模态不会正确更新

browser - Meteor 要求(客户端和服务器)

java - 如何将 log4j 日志记录到现有的 J2EE Struts Web 应用程序?

c - 忽略与 gcc 不兼容的指针类型(char**→void**)

ios - 我可以检查现在是否显示任何 UIAlertView 吗?

iphone - 为什么在 iPhone 或 iPad 设备上时 png 格式会发生变化