Swift String hasSuffix错误

时间:2015-12-06 22:08:50

标签: linux string swift

String.hasSuffix似乎没有在Linux上的Swift 2.2-dev中运行,在脚本中。它适用于REPL。我错过了什么?

  

cat hasSuffix.swift

import Foundation
let vegetable = "red pepper"
print(vegetable.hasSuffix("pepper"))
  

swift hasSuffix.swift

LLVM ERROR: Program used external function '_TFE10FoundationSS9hasSuffixfSSSb' which could not be resolved!
  

swift -version

Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c)
Target: x86_64-unknown-linux-gnu

1 个答案:

答案 0 :(得分:1)

替代

swift命令由于某些原因确实被您的脚本混淆了,但是同一个脚本在使用swiftc编译后才能运行:

  

$ swiftc hasSuffix.swift

     

$ ./hasSuffix

调试

swiftc实际上只是swift可执行文件的链接。

但是swift应用可以判断它是否已使用swiftswiftc启动,并且表现不同。

我们知道,swiftc启动编译器,swift启动REPL或“动态”编译脚本。

使用-driver-print-jobs作为swiftswiftc的选项,我们可以看到“编译器”模式和“动态”模式之间的差异:

  

$ swift -driver-print-jobs hasSuffix.swift

它打印已启动的实际命令:

  

... / swift / usr / bin / swift -frontend -interpret hasSuffix.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -color-diagnostics -module-name hasSuffix

现在使用swiftc

  

$ swiftc -driver-print-jobs hasSuffix.swift

结果:

  

... / swift / usr / bin / swift-autolink-extract /tmp/hasSuffix-d2b2b2.o -o /tmp/hasSuffix-f2d0a9.autolink

     

/ usr / bin / clang ++ /tmp/hasSuffix-d2b2b2.o -L ... / swift / usr / lib / swift / linux -Xlinker -rpath -Xlinker ... / swift / usr / lib / swift / linux -lswiftCore @ / tmp / hasSuffix-f2d0a9.autolink -Xlinker -T ... / swift / usr / lib / swift / linux / x86_64 / swift.ld -o hasSuffix

因此我们可以猜测,这些不同的编译选项是您的简单脚本无法与swift一起使用的原因:与使用其他选项启动时相比,某些东西丢失了。

正如Apple在他们的博客中所说,开源Swift仍然是一项正在进行中的工作 - 许多组件尚未实施,并且需要修复许多错误。