gpt4 book ai didi

ios - 基于时间的问候应用程序无法正常工作, swift

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

所以我正在尝试快速制作一个简单的基于时间的问候应用程序,但它不起作用,当我在我的应用程序的文本字段中按回车键时,它只是卡住,Xcode 将我带到 AppDelegate并且有一行上面有一个小标记,我不知道出了什么问题,为什么。

Xcode 说我的代码在编辑时没有错误,但应用程序无法运行。这是我的代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var nameLabel: UILabel!

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

@IBAction func nameTextField(nameTextField: UITextField) {
var currentTimeOfDay = ""

let hour = NSCalendar.current.component(.hour, from: NSDate() as Date)
if hour >= 0 && hour < 12 {

currentTimeOfDay = "Morning"

} else if hour >= 12 && hour < 17 {

currentTimeOfDay = "Afternoon"

} else if hour >= 17 {

currentTimeOfDay = "Evening"

}

nameLabel.text = "Good \(currentTimeOfDay) \(nameTextField)"
}
}

感谢任何帮助!

编辑:我不再需要答案,因为我只是在玩 Swift。无论如何,还是要感谢所有回答或评论的人!

-驼鹿

最佳答案

这是我的完整工作应用。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var helloLbl: UILabel!
@IBOutlet weak var nameText: UITextField!

var heyName = "Hey"
var greeting = ""


func greetingLogic() {
let date = NSDate()
let calendar = NSCalendar.current
let currentHour = calendar.component(.hour, from: date as Date)
let hourInt = Int(currentHour.description)!

if hourInt >= 12 && hourInt <= 16 {
greeting = "Good Afternoon"
}
else if hourInt >= 7 && hourInt <= 12 {
greeting = "Good Morning"
}
else if hourInt >= 16 && hourInt <= 20 {
greeting = "Good Evening"
}
else if hourInt >= 20 && hourInt <= 24 {
greeting = "Good Night"
}
else if hourInt >= 0 && hourInt <= 7 {
greeting = "You should be sleeping right now"
}

helloLbl.text = greeting
}

override func viewDidLoad() {
super.viewDidLoad()

greetingLogic()

}


@IBAction func sayHello(_ sender: UIButton) {

helloLbl.text = greeting + ", " + nameText.text!.capitalized + "!"

}

}

关于ios - 基于时间的问候应用程序无法正常工作, swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39562187/

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