How can I compare two different types of data (String and Int) using readLine() on Swift? - 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 internet 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)")
}

Related

How to remove a variable from the field of view

while true {
print ( """
1. Log in
2. Create new user
3. Quit
""")
if let numberString = readLine(), let number = Int(numberString) {
print("you entered \(number)")
break
} else {
print("Try again")
}
}
It is necessary to use the number variable in the future to compare it.
How can it be taken out of sight?
When I try to make a global var something goes wrong:
var numberString: String?
var number: Int?
while true {
print ( """
1. Log in
2. Create new user
3. Quit
""")
if numberString = readLine(), number = Int(numberString) {
print("you entered \(number)")
break
} else {
print("Try again")
}
}
Considering that you are creating a command prompt and that choice has no meaning outside your loop there is no need to make it global. You only need to switch the user selection and decide what to do from there. Note that if you try to break from inside the switch you won't exit your loop as I showed to you in your last question. To allow the compiler to know that you want to break the while loop instead of the switch you need to label your while loop statement, this way you can specify what you want to break when you have a switch inside a loop. Try like this:
func getValue() -> Int? {
guard let line = readLine(), let value = Int(line) else {
return nil
}
return value
}
question: while true {
print("""
1. Log in
2. Create new user
3. Quit
""")
guard let value = getValue() else {
continue
}
switch value {
case 1:
print("you have selected number one")
case 2:
print("you have selected number two")
case 3:
print("Good bye")
break question
default:
print("Try again")
}
}
There is not much information to go off of here, but I think I can help. In the first script, you are creating a new variable and setting the value for that variable:
if let numberString = readLine(), let number = Int(numberString) { ... }
In your second script, you have to global variables, but at no point have you provided them a value. What you need to do is provide both global variables (numberString and number) a value before comparing them. As such, you need to use the == sign to compare different variables/ types. For instance:
var numberString: String?
var number: Int?
while true {
print("""
1. Log in
2. Create new user
3. Quit
""")
numberString = readLine()
number = Int(numberString!)
}
You can now compare and or print your global variables...
Final Code
var numberString: String?
var number: Int?
while true {
print("""
1. Log in
2. Create new user
3. Quit
""")
numberString = readLine()
number = Int(numberString!)
if number == 1 {
print("you have selected number one")
} else if number == 2 {
print("you have selected number two")
} else if number == 3 {
print("you have selected number three")
} else {
print("Try again")
}
}
Due to the fact that both variables numberString and number are now global variables, you can access them in the future if you need to compare them again.

Array search function not working in Swift

I confess feeling little stupid, but after hours of trying I have to ask:
class AGE {
static func getAge() -> Int {
var age: Int
for items in dataArray {
if items.name == "Steven" {
age = items.age
}
else {
age = 0
}
}
// variable "age" used before being initialized - WHY?
return age
}
}
I even tried to set var age: Int = 0 at the beginning, but then the function return 0. I hope someone could forgive me this basic question at this point. Any help appreciated.
To make it more clear how dataArray looks like:
struct Person {
let name: String
let lastName: String
let age: Int
}
let person1: Person = Person(name: "Steven", lastName: "miller", age: 23)
let person2: Person = Person(name: "jana", lastName: "drexler", age: 31)
let person3: Person = Person(name: "hanna", lastName: "montana", age: 56)
var dataArray = [Person]()
dataArray.append(person1)
dataArray.append(person2)
dataArray.append(person3)
Update
Trying to assemble the essence of all answers, the solution has to look like this:
class AGE {
static func getAge() ->Int {
var age: Int = 0
for items in dataArray {
while items.firstName == "Steven" {
age = items.age
return age
break // This break will never be executed because of return.
}
break // setting the break here, the loop will break after first round
}
return age
}
}
This code works, (with the second break) but only for the first loop. The question remaining is, how to set up return and break after the loop hits its target. Either return or break will prevent the other step.
The possible program flow paths leading to the initialization of age before its return are not exhaustive. In case dataArray is empty, the body of the for ... in loop will never be entered, and age will never initialized. The simplest approach to resolve this issue in your particular example would be simply initialize age to 0 upon it's declaration.
Regarding the "unexpected" 0 return even in case dataArray contains an element with a name property valued "Steven": for each pass in the for ... in loop that does not contain an element with a name property valued "Steven", you will reset the value of age to 0. This means that only if the "Steven" valued element is the last of the dataArray will the non-0 age value persist. I don't really see any reason to ever set age to 0 again beyond initialization, so you could resolve this by e.g. performing an early exit (break) from the for loop in case of a match. Alternatively, depending on what you want to achieve, simply return items.last_updated from the method upon first match (just note that early return statements could affect performance).
I'm not Swift developer but I doubt these logics are correct. Assuming you initialise age to 0 as suggested in other answers, after you find the element that matches "Steven" into the array, you should exit. Otherwise the next iteration could set age to zero again.
Remember, by default for statement iterates through all elements. If you want to iterate till a condition is met, then a while may be more suitable.
I've read a tutorial and tested this code on line using swift sand box. Sharing my code on this link.
class AGE {
static func getAge() -> Int {
var age = 0
var found = false
var current = 0
var allElementsChecked = (dataArray.count == 0) //is false unless array is empty
while !found && !allElementsChecked {
found = (dataArray[current].name == "Steven")
allElementsChecked = (current == dataArray.count-1)
current += 1
}
if found
{
age = dataArray[current-1].age //minus 1 to undo last increment within loop
}
return age
}
}
When comes out from the loop that may be because either "Steven" was found or all elements were checked and not found.
As mentioned in the other answers the issue is that the variable will never be set if the array is empty.
Apart from that your code is inefficient and returns only the expected result if Steven is the last item in the array. Otherwise the variable age gets overwritten with 0.
Nevertheless in Swift there are more convenient methods to filter an item in an array than a repeat loop, for example first(where:
class AGE {
static func getAge() -> Int {
return dataArray.first(where: { $0.name == "Steven" })?.age ?? 0
}
}
or it might be preferable to return nil if Steven is not in the array
class AGE {
static func getAge() -> Int? {
return dataArray.first(where: { $0.Name == "Steven" })?.age
}
}
PS: last_updated in your code is not related to the struct Person at all.
You need to initilize it in the beginning:
var age: Int = 0
If for some reason your for-loop don´t execute then you can´t set your age variable when you only have declared it as var age: Int and not initilized it.
And regarding:
I even tried to set var age: Int = 0 at the beginning, but then the
function return 0
That means that you had intilizied your age variable to 0 and the for-loop where never executed and the return will be 0.
I tested Vadian's answer and it seems to be the correct one as per your requirements.
Nonetheless, here are other suggestions:
First, create another instance of your array based on your Person Struct:
var filteredPerson = [Person]()
If you want to search your dataArray for the age based on the first name:
func getAge(of name: String){
filteredPerson = dataArray.filter { $0.name == name }
print (filteredPerson[0].age) }
To get the age value of "Steven":
getAge(of: "Steven")
You'll get the Result: 23.
Note that you can also just get the Int value via return:
func getAge(of name: String) -> Int {
filteredPerson = dataArray.filter { $0.name == name }
return filteredPerson[0].age }
On the other hand, if you have lots of Persons in your list with the same name, and you want to get the age value of a specific Person, just add the lastName parameter:
func getAge(name: String, lastName: String){
filteredPerson = dataArray.filter { $0.firstName == name && $0.lastName == lastName }
print (filteredPerson[0].age) }
And then get the age value via calling:
getAge(name: "Steven", lastName: "Miller")
Hope this helped someone in some way :)

finding a number in a range swift 3

I have a number that I get from JSON, this number represents an age. Users give me a range of two ages and may code is supposed to check if this number I'm getting from JSON is in the range.
here is my code and it gives me error
Type of Expression is ambiguous without more context?
let age = "40"
if Int(AgeFrom) ... Int(AgeTO) ~= Int(age) {
print("yes")
}
Update
if let value: AnyObject = response.result.value as AnyObject? {
var ages = String
let json = JSON(value)
for (key, subJson) in json {
ages.append(subJson["age"].string!)
}
guard let min = Int(self.DropDownFrom.selectedItem!) else { return }
guard let max = Int(self.DropDownTo.selectedItem!) else { return }
for fitage in ages {
switch ages
{
case (min...max):
print ("Age is in range")
default:
print ("Nope, not this time")
}
}
Still gives me an error.
You need to unwrap the optionals because the Int(:String) method might not have a valid answer.
Best way to do this is kind of thing is with guard
guard let min = Int(AgeFrom) else { return }
guard let max = Int(AgeTo) else { return }
And from there you can go with the simple if statement:
if (min <= age && age <= max)
{
print ("Age is in range")
}
or get really fancy and use the switch statement pattern matching syntax (which I much prefer)
switch age
{
case (min...max):
print ("Age is in range")
default:
print ("Nope, not this time")
}
if - simplicity and readability
It is a basic thing in programming, checking if an optional is between two other optional values with an if:
if Int(AgeFrom)! <= Int(age)! && Int(AgeTO)! >= Int(age)! {
print("It is in the range!")
}
switch - multiple cases handling
However, I recommend using a switch for case handling:
switch(Int(AgeFrom)! <= Int(age)!, Int(AgeTO)! >= Int(age)!){
case (true,true): print("Yes, it fits the range")
case (false,true): print("Too young!")
case (true,false): print("Too old!")
}
The second solution is far better for multiple cases of the age value, especially when it's outside the range.
Hope it helps!
You can also use optional binding:
if let ageFrom = Int(ageFrom),
let ageTo = Int(ageTo),
ageFrom...ageTo ~= age
{
print("yes")
} else {
print("no")
}
You have to unwrap the optionals:
if Int(AgeFrom)!...Int(AgeTO)! ~= Int(age)! {
print("yes")
}
of course that is the unsafe way of unwrapping, since it will crash if the conversion of AgeFrom, AgeTO or age fail.

know the Datatype in Swift

i am new to swift i just started with the basics. In one of the Blog i saw a simple task which goes like this read a line from the stdin and check whether it is a integer,float,String.
I tried with the following code
let input = readLine()
var result = test(input)
print (result)
func test (obj:Any) -> String {
if obj is Int { return "This input is of type Intger." }
else if obj is String { return "This input is of type String." }
else { return "This input is something else. " }
}
when the input of 3245 is given it stores in the string format. and returns output as string.
how to overcome it..?
The readLine function returns a value of type String?. So your input variable can only be a String. It will never be Int or anything else.
If you want to see if the entered value is a valid number, you can try to convert the string to an Int.
if let input = readLine() {
if let num = Int(input) {
// the user entered a valid integer
} else {
// the user entered something other than an integer
}
}
As others have pointed out, readline() always returns a String?. It's up to you to parse that into whatever format you use it.
This is how I would do this:
let line = readLine()
switch line {
case let s? where Int(s) != nil:
print("This input is of type Intger.")
case let s? where Float(s) != nil:
print("This input is of type Float.")
case let s? where s.hasPrefix("\"") && s.hasSuffix("\""):
print("This input is of type String.")
default: print("This input is something else. ")
}
It exploits the ability of Int and Float's initializers to test the validity of a String, which almost entirely defeats the purpose of this exercise. But hey, it works, right? 😄
You can find of the type of object as
if let intt = obj as? Int {
// obj is a String. Do something with intt
}
else if let str = obj as? String {
// obj is a String. Do something with str
}
else {
//obj is something else
}

Swift Optionals - Variable binding in a condition requires an initializer

I am new to Swift and trying to figure out the Optional concept. I have a small piece of code in Playground which is giving me "Variable binding in a condition requires an initializer" error. Can someone please explain why and how do I fix it?
I only want to print "Yes" or "No" depending on if "score1" has a value or not. Here is the code:
import Cocoa
class Person {
var score1: Int? = 9
func sum() {
if let score1 {
print("yes")
} else {
print("No")
}
}//end sum
}// end person
var objperson = person()
objperson.sum()
The if let statement takes an optional variable. If it is nil, the else block or nothing is executed. If it has a value, the value is assigned to a different variable as a non-optional type.
So, the following code would output the value of score1 or "No" if there is none:
if let score1Unwrapped = score1
{
print(score1Unwrapped)
}
else
{
print("No")
}
A shorter version of the same would be:
print(score1 ?? "No")
In your case, where you don't actually use the value stored in the optional variable, you can also check if the value is nil:
if score1 != nil {
...
}
Writing
if let score1 {
doesn't make sense. If you want to see if score has a value, use
if score1 != nil {
or
if let score = score1 {
The last case binds a new non-optional constant score to score1. This lets you use score inside the if statement.
The code in your question is similar to something I saw in the swift book and documentation and they are correct.
Your playground is just using an old version of swift which currently doesn't support this syntax. Using a beta version of XCode should fix
https://www.reddit.com/r/swift/comments/vy7jhx/unwrapping_optionals/
the problem is that if let assumes you want to create a constant score1 with some value. If you just want to check if it contains a value, as in not nil you should just do it like below:
if score1! != nil {
// println("yes")
So your full code would look like this:
class Person {
var score1: Int? = 9
func sum() {
if score1 != nil {
println("yes")
}
else {
println("no")
}
}
}
var objperson = Person()
objperson.sum()
You can unwrap it using this:
import Cocoa
class Person {
var score1: Int? = 9
func sum() {
print("\(score1 != nil ? "YES" : "NO")")
}
}
And then call it like:
var objperson = Person()
objperson.sum()