UIDatePicker 问题 - 无法正确显示时间
UIDatePicker problems - not displaying time correctly
我想将用户从 UIDatePicker 中选择的时间保存到 String 类型的核心数据实体中(我只是选择了 String 以适合下面的代码。在此处打开以获取建议)。目的是稍后使用 this code 在选定的时间向用户发送内部通知。
当用户选择 20:00 并点击保存按钮时,我希望显示 20:00 .但是,它显示:
<NSDateFormatter: 0x60000024f8a0>
在 table 的行中。每添加一个时间,输出如上,而不是显示实际时间。我的目标只是存储时间,以便我可以向他们发送 this.
之类的通知
这是按钮的代码:
@IBAction func addBobBtnTapped(_ sender: AnyObject)
{
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext //creates an object of a property in AppDelegate.swift so we can access it
let bob = Bob(context: context)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm"
dateFormatter.string(from: timeSetbyUser.date)
bob.timeToPing = dateFormatter.description
//timeSetbyUser.date as NSDate?
//Save the data to core data
(UIApplication.shared.delegate as! AppDelegate).saveContext()
navigationController!.popViewController(animated: true)
}
非常感谢!
您需要替换以下两行:
dateFormatter.string(from: timeSetbyUser.date)
bob.timeToPing = dateFormatter.description
与:
bob.timeToPing = dateFormatter.string(from: timeSetbyUser.date)
不要忽略警告。您应该会在行中看到一条警告:
dateFormatter.string(from: timeSetbyUser.date)
关于未使用的 return 值。这是一个重要的线索。
您还应该更改日期格式。使用 HH:mm
表示 24 小时制或使用 hh:mm a
表示 12 小时制 am/pm.
我想将用户从 UIDatePicker 中选择的时间保存到 String 类型的核心数据实体中(我只是选择了 String 以适合下面的代码。在此处打开以获取建议)。目的是稍后使用 this code 在选定的时间向用户发送内部通知。
当用户选择 20:00 并点击保存按钮时,我希望显示 20:00 .但是,它显示:
<NSDateFormatter: 0x60000024f8a0>
在 table 的行中。每添加一个时间,输出如上,而不是显示实际时间。我的目标只是存储时间,以便我可以向他们发送 this.
之类的通知这是按钮的代码:
@IBAction func addBobBtnTapped(_ sender: AnyObject)
{
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext //creates an object of a property in AppDelegate.swift so we can access it
let bob = Bob(context: context)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm"
dateFormatter.string(from: timeSetbyUser.date)
bob.timeToPing = dateFormatter.description
//timeSetbyUser.date as NSDate?
//Save the data to core data
(UIApplication.shared.delegate as! AppDelegate).saveContext()
navigationController!.popViewController(animated: true)
}
非常感谢!
您需要替换以下两行:
dateFormatter.string(from: timeSetbyUser.date)
bob.timeToPing = dateFormatter.description
与:
bob.timeToPing = dateFormatter.string(from: timeSetbyUser.date)
不要忽略警告。您应该会在行中看到一条警告:
dateFormatter.string(from: timeSetbyUser.date)
关于未使用的 return 值。这是一个重要的线索。
您还应该更改日期格式。使用 HH:mm
表示 24 小时制或使用 hh:mm a
表示 12 小时制 am/pm.