我试着深入研究一下swift,开始尝试做一些简单的事情,但我不断得到这样的错误:“致命错误:在打开可选值时意外地发现了nil”(what fatal error:unwrapping an Optional value)
我在这里和谷歌上看了看,试过他们让我做的事情,但它不起作用,我觉得自己很愚蠢。
这是我的代码:
import Foundation
import Glibc
print("How much did you spend today: ")
if let input = Int(readLine()!){
print("You have spent", input)
}
else{
print("The input is not a number")
}
https://www.journaldev.com/19612/swift-readline-swift-print是一个很好的教程,可以让新手做第一步...
试试看:
import Foundation
import Glibc
print("How much did you spend today: ")
if let input = readLine(){
if let intputInt = Int(input) {
print("You have spent", inputInt)
}
else {
print("The input is not a number")
}
}
else{
print("The input is not valid")
}