gpt4 book ai didi

swift - 尝试制作一个小型 ncurses 应用程序,但出现错误 : expressions are not allowed at the top level

转载 作者:行者123 更新时间:2023-11-28 06:14:46 32 4
gpt4 key购买 nike

按照 iachievedit 的说明进行操作并使用 mccuneware的 ncurses 包我无法使以下代码无错误地运行。

import Foundation
import CNCURSES
import Glibc

enum Signal:Int32 {
case INT = 2
case WINCH = 28
}

typealias SignalHandler = __sighandler_t

func trap(signum:Signal, action:SignalHandler) {
signal(signum.rawValue, action)
}

trap(.INT) { signal in
endwin()
exit(0)
}

initscr()
noecho() // Turn on noecho, though it doesn't matter in this example
curs_set(0) // 0 is invisible, 1 is visible, 2 is very visible

move(0, 0)
addstr("UL")
refresh() // This is required to update the screen
move(23,78)
addstr("LR")
refresh()

select(0, nil, nil, nil, nil) // Wait

错误是:

Compile Swift Module 'ncurses' (1 sources)
/home/luis/Dropbox/Documentos/Coding/Swift/ncurses/Sources/ncurses.swift:17:1: error: expressions are not allowed at the top level
initscr()
^

目录结构为:

ncurses/Package.swift

ncurses/Sources/ncurses.swift

前面提到的 CNCURSES 在 Package.swift 中被调用:

// swift-tools-version:3.1

import PackageDescription

let package = Package(
name: "ncurses",
dependencies: [
.Package(url: "https://github.com/mccuneware/CNCURSES", majorVersion:1),
]
)

在我看来,说明已经过时了,但是如何避免错误呢?

最佳答案

表达式确实不能位于模块的顶层。你必须将这些写在 main.swift 文件中,这是编译器所期望的。 main.swift 文件是程序执行的入口点。

准确地说:包含根级指令的文件必须命名为main.swift,而不是其他任何东西;当然,可以存在其他文件,但它们不能包含顶级指令。

关于swift - 尝试制作一个小型 ncurses 应用程序,但出现错误 : expressions are not allowed at the top level,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45502987/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com