我正在制作一个命令行应用程序来学习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("")
printArray(theArray:[AnyObject]):
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())
}
INTVAL
extension String
{
var intValue:Int
{
return Int(self)!
}
}
答案 0 :(得分:2)
尝试
repeat
{
fflush(stdout)
print("Welcome to the login system")