Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么会导致不同文件中的Swift func无法为iOS解析?_Ios_Swift_Macos - Fatal编程技术网

什么会导致不同文件中的Swift func无法为iOS解析?

什么会导致不同文件中的Swift func无法为iOS解析?,ios,swift,macos,Ios,Swift,Macos,我有几个Swift文件,每个文件都属于一个iOS和一个OS X目标: 文件A public func doThing() { let constant: CustomValue? = otherThing { //... } } 文件B public func otherThing(block: () -> Void) -> CustomValue? { //... } 当我编译并运行OSX时,一切正常。不过,为iOS编译时会抱怨文件A:

我有几个Swift文件,每个文件都属于一个iOS和一个OS X目标:

文件A

public func doThing() {
    let constant: CustomValue? = otherThing {
        //...
    }
}
文件B

public func otherThing(block: () -> Void) -> CustomValue? {
    //...
}
当我编译并运行OSX时,一切正常。不过,为iOS编译时会抱怨文件A:

使用未解析标识符“otherThing”

为了排除故障,我在上面的
doThing()
文件中实现了一个
otherThing()
存根。文件A已编译,但文件B出现以下错误:

“otherThing”的重新声明无效

现在,如果它没有在文件A中解析它,那么如何在文件B中“重新声明”它呢?如何从文件B中获取文件A以解析
其他内容


要查看我遇到问题的特定项目,请查看(修订版
917be3b8175576ae7e0c6275d388718fcc465040
)。您只需要克隆(递归地,因为有一个子模块)并构建iOS方案。

问题在于
catchBadInstruction
函数是在条件编译块中声明/定义的:

#if arch(x86_64)
// ...
public func catchBadInstruction(block: () -> Void) -> BadInstructionException? {
    // ...
}
// ...
#endif
因此,在iOS上,它不编译,也不存在

要确定这是真的,请在条件编译块之外声明一个如下的存根:

public func catchBadInstructionn(block: () -> Void) -> BadInstructionException? {
    return nil
}
请注意,它有一个不同的名称(名称末尾有两个“n”),因此不会出现“重新声明”错误。现在返回ThrowAssertion.swift并用对该函数的调用替换该调用:

let caughtException: BadInstructionException? = catchBadInstructionn // ...

它编译得很好-因为
catchBadInstructionn
存在且可见,因为它不在条件编译块内。如果将其移动到条件编译块中,编译将以与以前完全相同的方式失败,证明条件编译确实是问题所在。

这意味着您在两个位置说了
func otherThing
。你不能这么做。@matt我的目标不是重新声明
其他东西,而是从文件A中解决它。我已经更新了问题以使其更清楚。你不需要做任何事情就可以将问题从一个文件“解决”到另一个文件。所有文件顶层的所有内容都会在同一模块中的所有其他文件中自动可见。@matt我知道我不需要做任何事情,但两个文件都在同一个目标中,函数无法解析。让我们来看看。谢谢,这就解释了。但奇怪的是,即使在我为>=iPhone 5s模拟器(应该是x86_64)构建时,我也会得到这样的结果……不过,您并不是只为x86_64编译。无论如何,我觉得我已经证明了我的答案是正确的,所以我希望你能接受。