在关于如何使用带有swift的ncurses的教程中,我遇到了错误:
50:5: error: missing argument label 'signum:' in call
trap(.INT) { signal in
^
signum:
在这个小小的尝试中捕获关闭终端的信号(据我所知):
trap(.INT) { signal in
endwin()
exit(0)
}
整个参考代码:
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)
}
func getmaxyx(window:UnsafeMutablePointer<WINDOW>, y:inout Int32, x:inout Int32) {
x = getmaxx(window)
y = getmaxy(window)
}
func getcuryx(window:UnsafeMutablePointer<WINDOW>, y:inout Int32, x:inout Int32) {
x = getcurx(window)
y = getcury(window)
}
func drawbox(numlines:Int32, numcols:Int32) {
for y in 0...numlines-1 {
for x in 0...numcols {
move(y, x)
if y == 0 || y == numlines-1 {
addch(UInt("*"))
} else {
if x == 0 || x == numcols {
addch(UInt("*"))
}
}
}
}
refresh()
}
func centerText(text:String, numlines:Int32, numcols:Int32) {
let cy:Int32 = numlines/2
let cx:Int32 = (numcols - Int32(text.characters.count))/2
move(cy,cx)
addstr(text)
refresh()
}
trap(.INT) { signal in
endwin()
exit(0)
}
var maxy:Int32 = 0
var maxx:Int32 = 0
trap(.WINCH) { signal in
endwin()
refresh()
initscr()
clear()
getmaxyx(stdscr, y:&maxy, x:&maxx)
drawbox(maxy, numcols:maxx)
centerText("Hello world!", numlines:maxy, numcols:maxx)
}
initscr()
noecho()
curs_set(0)
getmaxyx(stdscr, y:&maxy, x:&maxx)
drawbox(maxy, numcols:maxx)
centerText("Hello world!", numlines:maxy, numcols:maxx)
while true {
select(0, nil, nil, nil, nil)
}
答案 0 :(得分:1)
您应该在函数调用中命名参数signum
。将trap(.INT)
替换为
trap(signum:.Int)