gpt4 book ai didi

ios - 将代码中的文本分配给 UILabel! swift 2.2

转载 作者:搜寻专家 更新时间:2023-11-01 07:24:50 25 4
gpt4 key购买 nike

使用 iOS 8/Swift 1 中的教程创建示例应用程序,尝试使用 Xcode 7.3/Swift 2.2 将文本分配给标签。提前致谢。

import UIKit

class ViewController: UIViewController {

@IBOutlet var ageCat: UITextField!

@IBOutlet var resultAge: UILabel!

@IBAction func findAgeButton(sender: AnyObject) {

var enteredAge = Int(ageCat.text!)

var catYears = enteredAge! * 7

resultAge = "Your cat is \(catYears) in cat years"

}
}

最佳答案

将您的 IBAction func findAgeButton 替换为以下内容:

@IBAction func findAgeButton(sender: AnyObject) {

// Here the variable is unwrapped. This means the code after it is not executed unless the program is sure that it contains a value.
if let text = ageCat.text {
// You can use the unwrapped variable now and won't have to place an exclamation mark behind it to force unwrap it.
let enteredAge = Int(text)
if let age = enteredAge {
let catYears = age * 7
resultAge.text = "Your cat is \(catYears) in cat years"
}
}
}

关于ios - 将代码中的文本分配给 UILabel! swift 2.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36832005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com