简体   繁体   中英

cannot assign value of type 'string' to type 'int'

So, getting started with Swift and dev of iOS apps.
Did a beginner tutorial converting dog age to human age.

It was really basic and just based age on multiple of 5, but i felt wasnt accurate. (hence all ugly if else conditions)

I have two questions:

  1. What is the best way to accomplish the same calculation on age but minimize all the if else statements? Theres gotta be a better way.

  2. The last if else statement i have for 30 years is supposed to assign dogAge variable with a string. I am having troubles converting the var from int to string and have been trying various things ive researched and so far, no dice.

Thanks in advance!

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var dogAgeTextField: UITextField!

@IBOutlet weak var resultLabel: UILabel!

@IBAction func reset(sender: AnyObject) {
   resultLabel.text = " "
   dogAgeTextField.text=" "
}
@IBAction func findAge(sender: AnyObject) {

    var dogAge = Int(dogAgeTextField.text!)!

    if (dogAge == 1){dogAge=15}
     else if (dogAge == 2) {dogAge=24}
     else if (dogAge == 3) {dogAge=29}
     else if (dogAge == 4) {dogAge=34}
     else if (dogAge == 5) {dogAge=38}
     else if (dogAge == 6) {dogAge=42}
     else if (dogAge == 7) {dogAge=47}
     else if (dogAge == 8) {dogAge=51}
     else if (dogAge == 9) {dogAge=56}
     else if (dogAge == 10) {dogAge=60}
     else if (dogAge == 11) {dogAge=65}
     else if (dogAge == 12) {dogAge=69}
     else if (dogAge == 13) {dogAge=74}
     else if (dogAge == 14) {dogAge=78}
     else if (dogAge == 15) {dogAge=83}
     else if (dogAge == 16) {dogAge=87}
     else if (dogAge == 17) {dogAge=92}
     else if (dogAge == 18) {dogAge=96}
     else if (dogAge == 19) {dogAge=101}
     else if (dogAge == 20) {dogAge=105}
     else if (dogAge == 21) {dogAge=109}
     else if (dogAge == 22) {dogAge=113}
     else if (dogAge == 23) {dogAge=117}
     else if (dogAge == 24) {dogAge=121}
     else if (dogAge == 25) {dogAge=125}
     else if (dogAge == 26) {dogAge=dogAge*5}
     else if (dogAge == 27) {dogAge=dogAge*5}
     else if (dogAge == 28) {dogAge=dogAge*5}
     else if (dogAge == 29) {dogAge=dogAge*5}
     else if (dogAge >= 30) {var dogAge=String(UTF8String: "really old")!}


    resultLabel.text = "Your dog is \(dogAge) in human years."
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

The issue is you cannot assign a String to variable declared as Int

The whole expression can be simplified using an array to map the dog/human years.

let dogToHuman = [0, 15, 24, 29 ... 140, 145] // complete the array with the missing values

if let dogAge = Int(dogAgeTextField.text!) {

   let humanAge : String

   if dogAge >= 30 {
     humanAge = "really old"
   } else {
     humanAge = String(dogToHuman[dogAge])
   }
   resultLabel.text = "Your dog is \(humanAge) in human years."
} else {
   resultLabel.text = "The input is not an integer"
}
  1. What is the best way to accomplish the same calculation on age but minimize all the if else statements? Theres gotta be a better way.

One naive implementation is to put all of the dog year values in an array, where each one's index corresponds to the human years. For example,

let humanYears = [0, 15, 24, 29, 34, 38, 42, 47, 51, 56, 60, 65, 69, 74, 78, 83, 87, 92, 96, 101, 105, 109, 113, 117, 121, 125]
let humanAgeString: String

if 1 <= dogAge && dogAge < 26 {
    humanAgeString = "\(humanYears[dogAge])"
} else if 26 <= dogAge && dogAge < 30 {
    humanAgeString = "\(dogAge * 5)"
} else if dogAge >= 30 {
    humanAgeString = "really old!"
} else {    // negative values or 0?
    humanAgeString = "impossible to calculate"
}

resultLabel.text = "Your dog is \(humanAgeString) in human years."

Note that array index 0 is just 0 , so that the rest of the array is 1-based.

You could try to come up with a formula that will calculate things a little more elegantly, but for this particular problem set, that seems like overkill.

The last if else statement i have for 30 years is supposed to assign dogAge variable with a string. I am having troubles converting the var from int to string and have been trying various things ive researched and so far, no dice.

To put an Int into a String , just use the inline string variable substitution, like "Dog age: \\(dogAge)" .

If you want a formula to calculate this, it looks like 5x + (15 - floor(x / 2)) will give you a pretty good approximation when x <= 20 .

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