ios - 命令行 C 程序到 Swift

标签 ios c swift command-line-interface

我有一个命令行C 游戏 的源代码,它等待用户的stdin,并输出stdout。我想将文本字段中的输入提供给此 C 代码的 main,并以文本区域字段中的输出为例。

int mainer(int argc, char **argv) {
    int i;
    int hitme;
    char ch;
    prelim();

    if (argc > 1) { // look for -f option
        if (strcmp(argv[1], "-f")== 0) {
            coordfixed = 1;
            argc--;
            argv++;
        }
    }


    if (argc > 1) {
        fromcommandline = 1;
        line[0] = '\0';
        while (--argc > 0) {
            strcat(line, *(++argv));
            strcat(line, " ");
        }
    }
    else fromcommandline = 0;
 // some other code

}

来自示例here ,我应该执行以下操作:

let args = [" ", "regular", "short", ""]

var cargs = args.map { strdup($0) }
let result = mainer(Int32(args.count), &cargs)
for ptr in cargs { free(ptr) }

我如何调用 main 函数,并使其保持事件状态并不断为其提供参数以使其充当命令行

最佳答案

我会使用 TextArea 和 TextField 设置您的应用程序,并提供 printf 和 scanf 的替代方法,它们可以将文本添加到文本区域或从文本字段获取发送的文本(可能带有“Enter”UIButton)。然后你可以编译 C 代码并从 Swift 调用它。 Objective C 实际上是幕后的 C 方式,因此您只需稍加改动就可以让它运行。

为了让代码运行,您可能必须让这段代码在它自己的线程中运行,并跳过一些环节以使这些调用正确运行,但这应该是可行的。

关于ios - 命令行 C 程序到 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34293161/

相关文章:

objective-c - 如何访问 C 函数中的 N 个参数并将相同的 N 传递给下一个?

ios - 自定义动画 : easing out

ios - 使用 Storyboard segues 将 View Controller 从模态推送到模态父级使用的导航 Controller

ios - CALayer 内边框圆角半径

ios - 如果框架不存在,Objective-c 在运行时关闭类/代码块

c popen 不会捕获 stderr

c - 为什么这个堆栈程序在使用 POP 时给出垃圾值?

ios - 如何判断 iPhone 是 "face down"还是 "face up"?

ios - 现有 CollectionView 图像在滚动时发生变化

swift - 将字符串分配给字典