简体   繁体   中英

How can I compare two different types of data (String and Int) using readLine() on Swift?

everyone, I'm a new member of Stack Overflow. just like I'm beginner on swift programming: I'm making this post to find out a solution for the following case:

I'm creating on Swift an app using the Command Line Tool for inputing data. The app basically works as an authenticator. As an example, if someone types USA for the country name and the age is 17, so the program will return a message like "You can not apply to this position". Otherwise, if the country name is USA and the age is equal or higher than 18, so the message returns is "You can forward to the next step". I've tried many times to set this conditions, but it's not working. I'm already knows that the function readLine() is an Optional String, but how can I make my program work correctly? It follows my code above to you understanding my thoughts.

I really appreciate any help. Again, I'm beginner and I'm already studying Swift languages, but I'm seeking some solution that handles Integers and Strings and comparing both data types. Thank you very much!

My code is:

import Foundation

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var age = readLine()

if var country, var age = readLine(){
    if country == "USA" && age < "18" {
        print("You're not allowed to apply to this position.")
    } else {
        print("You can forward to the next step.")
    }
    
}


PS: As you see, I'm using wrongly the variable age as an String, but I want to convert it to an Int type and then, check if the country name is the same than the value I assigned to or the age is equal or higher than 18. But not found a solution so far.

I'm trying to find a solution that compares two different types on Swift, using Command Line Tool and the readLine() function to check if a condition is true or not. If it's true, an output message will show that the user can proceed to the next step, otherwise he will not be permitted to follow. I'm keeping for an explanation on inte.net since few days, but not found anything that might help me. I hope to get some help using the Stack Overflow forum to some useful answer.

First one, readline() means you read the current to the end of current line. As your code when you check condition you call readLine() again. The wrong part is here.

I recommend you to read first then do all your logic. You just need to read only one time at first

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

Next, check if it is nil or not ( because option value which is value can be nil)

if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

Then check if can convert to Int or not. Reach here you sure that the ageString is not nil because you have checked above. So you just simply convert

guard let ageString = ageString else {
    print("Error age nil")
    fatalError()
}

guard let age = Int(ageString) else {
    print("Error age not a number")
    fatalError()
}

Then after all, you just check your condition

Full code will be like this

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

// check nil first if nil return or do something first and not get to the rest
if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

guard let ageString = ageString else {
    print("Error age nil")
    fatalError()
}

guard let age = Int(ageString) else {
    print("Error age not a number")
    fatalError()
}

if country == "USA" && age < 18 {
    print("You're not allowed to apply to this position.")
} else {
    print("You can forward to the next step.")
}

Other methods is use if let to achieve so no force unwrap

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var ageString = readLine()

// check nil first if nil return or do something first and not get to the rest
if country == nil || ageString == nil {
    print("Error because one of them is nil")
    fatalError()
}

if let ageString = ageString {
    if let age = Int(ageString) {
        if country == "USA" && age < 18 {
            print("You're not allowed to apply to this position.")
        } else {
            print("You can forward to the next step.")
        }
    } else {
        print("Error age not a number")
        fatalError()
    }
}

SOLUTION SOLVED!

Hey, guys, first of all I want to thank you all for your helpful answers, which helped me a lot. I've got finally a solution, and am going to share it with you.

What did I done? I just created two variables, one String and another Integer. Then, using the if var to force unwrapping of the Int variable, I've made an if statement to check if the both conditions are true (in the case, if the person is from USA and has an age equals or higher than 18). Now, the program is running on the same way I just wanted. If you are from USA but has no 18 years, output returns a message that you can not apply. Otherwise, you're able to forward.

I'll let the code above. If you want to make some comments or any suggestions, it'll be welcome.

Again, thank you very much for all your answers.

var countryCheck = "USA"
var ageCheck: Int = 18

print("Enter your country: ")
var country = readLine()

print("Enter your age: ")
var age = readLine()

if var countryCheck = country, var ageCheck = Int(age!) {
    if countryCheck == "USA" && ageCheck >= 18 {
        print("You can apply.")
    } else {
        print("You can not apply to this position.")
    }
}

I hope this help you:)

import Foundation

print("Enter your country: ")
if let country = readLine() {
  if let num = Int(country) {
      print(num)
      }
    }
      let country = readLine()
let age = readLine()


if let USA = country, 
    let num1 = Int(USA), 
    let num2 = Int(USA) {
    print("The age of \(num1) and \(num2) is \(num1 + num2)")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM