0

I'm trying to dive into swift a little bit and started trying to make something simple and I keep getting this error: "Fatal error: Unexpectedly found nil while unwrapping an Optional value"

I've looked around on here and google and tried what they told me to do and it doesn't work and I feel stupid af.

This is my code:

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")
}
FrogV1
  • 3
  • 3

1 Answers1

0

https://www.journaldev.com/19612/swift-readline-swift-print is a good tutorial for newbies to do the first steps...

Try this:

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")
}
Hardy_Germany
  • 1,259
  • 13
  • 19