ReadLine() - Very Common But Unsolvable Error "Unexpectedly found nil while unwrapping an Optional value"

Something wrong with Readline. I can't use

readLine()!)! 

My code is really simple:

var aYear = Int(readLine()!)!

func isLeap(year: Int) {
   
  var leap = "NO"
   
  //IF divisible by 4 with no remainders.
  if year % 4 == 0 {
    leap = "YES"
    //Is leap year, unless:
  }
  if year % 100 == 0 {
    leap = "NO"
    //Is not leap year, unless:
  }
  if year % 400 == 0 {
    leap = "YES"
    //Is leap year.
  }
  print(leap)  
}
//Don't change this
isLeap(year: aYear)

This error keeps coming up. I can't figure out how to fix this error:

"__lldb_expr_122/readline .playground:1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Playground execution failed:
error: Execution was interrupted, reason: EXC_BREAKPOINT (code=1, subcode=0x18f494588).
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation."

It is really strange because in online compiler this simple code works just fine.

PLEASE HELP ME. I CAN'T FIND THE SOLUTION. It's kinda internal xcode error or what...

How do you test ? In app, in playground ?

Where do you get the input for readLine ?

If you have no input, readLine returns nil, hence crash.

This works of course in playground:

let aYear = (0...3000).randomElement()! // Int(readLine()!)!

func isLeap(year: Int) {
   print("year:", year)
  var leap = "NO"
   
  //IF divisible by 4 with no remainders.
  if year % 4 == 0 {
    leap = "YES"
    //Is leap year, unless:
  }
  if year % 100 == 0 {
    leap = "NO"
    //Is not leap year, unless:
  }
  if year % 400 == 0 {
    leap = "YES"
    //Is leap year.
  }
  print(leap)
}

isLeap(year: aYear)

Thanks for your answer. I test it in playground, yep. The playground crashes and the playground doesn't even offer me to input numbers from the keyboard.

Yes, your code works just fine, but I want to make keyboard input...

This works:

class V: UIViewController, UITextFieldDelegate {

    var textField = UITextField(frame: CGRect(x: 100, y: 20, width: 200, height: 24))
    var label = UILabel(frame: CGRect(x: 20, y: 20, width: 60, height: 24))
    var doneButton = UIButton(frame: CGRect(x: 140, y: 120, width: 60, height: 24))
    var resultLabel = UILabel(frame: CGRect(x: 100, y: 80, width: 160, height: 24))

    
    var newValue: Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(label)
        view.addSubview(textField)
        view.addSubview(resultLabel)
        view.addSubview(doneButton)
        label.text = "Input"
        textField.backgroundColor = .systemYellow
        textField.delegate = self
        doneButton.setTitle("Test it", for: .normal)

        doneButton.backgroundColor = .systemBlue
        doneButton.addTarget(self, action: #selector(doneAction), for: .touchUpInside)
    }
    
    @objc func doneAction() {
        textField.resignFirstResponder()
    }

    // Avoid non numeric input
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        if textField.text == nil || (textField.text! != "" && Int(textField.text!) == nil)  {
            return false
        }

        return true
    }
    
    // When done, hit test to see result
    func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
        
        newValue = Int(textField.text ?? "0") ?? 0

        isLeap(year: newValue)
    }

    func isLeap(year: Int) {
        
        var leap = false
        
        //IF divisible by 4 with no remainders.
        if year % 4 == 0 {
            leap = true
            //Is leap year, unless:
        }
        if year % 100 == 0 {
            leap = false
            //Is not leap year, unless:
        }
        if year % 400 == 0 {
            leap = true
            //Is leap year.
        }

        resultLabel.text = leap ? "\(year) is leap " :  "\(year) is not leap "
    }
    
}

let vv = V()
vv.view.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
PlaygroundPage.current.liveView = vv.view
PlaygroundPage.current.needsIndefiniteExecution = true

Credit: https://stackoverflow.com/questions/41733588/how-to-get-input-from-user-using-swift-in-playground-project-in-xcode-8-2

ReadLine() - Very Common But Unsolvable Error "Unexpectedly found nil while unwrapping an Optional value"
 
 
Q