Ubuntu 14.04 中的 "swift build" 错误

Error with "swift build" in Ubuntu 14.04

我在尝试编译 Swift 代码时遇到问题,我认为它与 Ubuntu 中的 Swift 有关。这是错误:

swift build 

Compiling Swift Module 'testregex' (1 sources) /home/guillermo/Descargas/Hola/Sources/testregex.swift:4:1: error: statements are not allowed at the top level if let range = str.rangeOfString("string$", options: .RegularExpressionSearch) { ^ <unknown>:0: error: build had 1 command failures

error: exit(1): ["/home/guillermo/Descargas/swift-DEVELOPMENT-SNAPSHOT-2016-01-25-a-ubuntu14.04/usr/bin/swift-build-tool", "-f", "/home/guillermo/Descargas/Hola/.build/debug/testregex.o/llbuild.yaml"]

如果有人知道如何解决这个奇怪的问题,我将非常感激 :)

这是因为 swift build 期望在 Sources 文件夹中找到一个 main.swift 文件。

将您的 testregex.swift 文件重命名为 main.swift,它将正确构建。

您可以在 Sources 文件夹中拥有任意数量的 .swift 个文件,但必须有一个 main.swift 个文件。