swift2 Swift无法使用终止符参数打印到控制台

uwopmtnx  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(218)

我正在制作一个命令行应用程序来学习swift和OOP编程。我的代码在Xcode调试器中工作得很好(我看到的输入和输出都很好),但是当我存档和导出我的项目时,由于某种原因,我看不到print函数的输出。
这里有一个例子来说明我的意思。
在Xcode中调试:

Welcome to the login system
-----------------------------

Add a user    [0]
List users    [1]
Delete user   [2]
Quit System   [3]
-------------------------------
Option: 0
Username: User
Password: Pass
User: User sucsessully added to the system
Welcome to the login system
-----------------------------

Add a user    [0]
List users    [1]
Delete user   [2]
Quit System   [3]
-------------------------------
Option: 1
User
Welcome to the login system
-----------------------------

Add a user    [0]
List users    [1]
Delete user   [2]
Quit System   [3]
-------------------------------
Option: 0
Username: User2
Password: Pass
User: User2 sucsessully added to the system
Welcome to the login system
-----------------------------

Add a user    [0]
List users    [1]
Delete user   [2]
Quit System   [3]
-------------------------------
Option: 1
User
User2
Welcome to the login system
-----------------------------

Add a user    [0]
List users    [1]
Delete user   [2]
Quit System   [3]
-------------------------------
Option: 2
Delete user: User2
Welcome to the login system
-----------------------------

Add a user    [0]
List users    [1]
Delete user   [2]
Quit System   [3]
-------------------------------
Option: 1
User
Welcome to the login system
-----------------------------

Add a user    [0]
List users    [1]
Delete user   [2]
Quit System   [3]
-------------------------------
Option: 3

Program ended with exit code: 0

现在,当我在终端中运行存档的导出命令行应用程序时:

Welcome to the login system
-----------------------------

Add a user    [0]
List users    [1]
Delete user   [2]
Quit System   [3]
-------------------------------
0
User
Pass
Option: Username: Password: User: User sucsessully added to the system
Welcome to the login system
-----------------------------

Add a user    [0]
List users    [1]
Delete user   [2]
Quit System   [3]
-------------------------------
3

正如你所看到的,程序注册我的输入,有时打印。
我试着把terminator: ""print函数中去掉,看看这是否是问题所在,它打印得很好。这似乎是终结符参数的问题。我也试着把终结符参数的""替换为" ",但它仍然不起作用。
为什么我不能用terminator参数输出到控制台?
下面是代码:

import Foundation

var running:Bool = true
let loginSystem = LoginSystem()

repeat
{
print("Welcome to the login system")
print("-----------------------------\n")
print("Add a user    [0]")
print("List users    [1]")
print("Delete user   [2]")
print("Quit System   [3]")
print("-------------------------------")
printToConsole("Option: ")
let option = input().intValue

//Add a user
if option == 0
{
    print("Username: ", terminator: " ")
    let username = input()
    print("Password: ", terminator: " ")
    let password = input()
    print("User: \(username) sucsessully added to the system")
    loginSystem.addUser(username, password: password, uid: 0, guid: 0)

//List users
} else if option == 1 {
    let users = loginSystem.getUsers()
    printArray(users)

//Delete user
} else if option == 2 {
    printToConsole("Delete user: ")
    let user = input()
    loginSystem.deleteUser(user)

//Quit
} else if option == 3 {
    running = false
}
clear()
} while running

print("")

打印数组(数组:[任意对象]):

func printArray(theArray:[AnyObject])
{
    for item in theArray
    {
        print("\(item)")
    }
}

清除()

func clear()
{
    let clearCommand = "if [ -n \"$STY\" ]; then clear; fi"
    system(clearCommand)
}

输入()

func input() -> String {
    let keyboard = NSFileHandle.fileHandleWithStandardInput()
    let inputData = keyboard.availableData
    let strData = NSString(data: inputData, encoding: NSUTF8StringEncoding)!
    return strData.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
}

整数值

extension String
{
    var intValue:Int
    {
        return Int(self)!
    }
}
qzwqbdag

qzwqbdag1#

尝试

repeat 
{
fflush(stdout)
print("Welcome to the login system")

相关问题