I am writing a program on Swift 4 console under MacOS 10.13.5 and for some reason the signs <, ==,> are an error I cite the error code Binary operator '<' cannot there are operators, but the program code itself:

import Cocoa import Darwin import Foundation var age: Int? = Int(20) //var age = 0 if let age = readLine() { if age < 5 { print ("Пора идти в садик") } else if age == 5 { print ("Пора идти в прескул") } else if (age > 5) && (age <= 18) { let grade = age - 5 print ("Пора идти в \(grade) класс") } else { print ("Пора идти в университет") } } 

 import Cocoa import Darwin import Foundation var age: Int? = Int(20) //var age = 0 age = readLine() { if age < 5 { print ("Пора идти в садик") } else if age == 5 { print ("Пора идти в прескул") } else if (age > 5) && (age <= 18) { let grade = age - 5 print ("Пора идти в \(grade) класс") } else { print ("Пора идти в университет") } } 
  • And it pops right. How are you going to compare the text with the number? - D-side
  • And what do you do please tell me - Sergey
  • I asked you a question, the answer to which will give you a solution. How do you want to compare them? - D-side
  • Enter the number in readLine and it is compared with age - Sergey
  • So you want to convert the result of readLine() (text) to a number. - D-side

1 answer 1

The first. Swift is a strongly typed language, so it’s impossible not only to compare a string with a number, but also numbers of different types, for example, Int and Double .

The second. The ability to group code fragments with curly {} brackets was before Swift 3, which is why the Cannot convert value of type '() -> ()' to expected argument type 'Bool' error Cannot convert value of type '() -> ()' to expected argument type 'Bool' error occurs Cannot convert value of type '() -> ()' to expected argument type 'Bool'

A little tweaked your example to make it work:

 print("Введите возраст") guard let input = readLine(), let age = Int(input) else { print("Неверный ввод. Повторите попытку") exit(0) } if age < 5 { print ("Пора идти в садик") } else if age == 5 { print ("Пора идти в прескул") } else if (age > 5) && (age <= 18) { let grade = age - 5 print ("Пора идти в \(grade) класс") } else { print ("Пора идти в университет") } 
  • Thank you very much, everything turned out! - Sergey