目前我想从解析数据库中检索createdAt 和price,两者的数据类型都是Number 和NSDate。检索到它后,我想传递给 UILabel,分别是 date 和 price。现在的问题是,每当我尝试运行模拟器时,两个 UILabel 数据都不会显示,而字符串数据类型会显示。
我当前的代码
if let createdAt = object?["createdAt"] as? String {
cell.date.text = createdAt
}
if let priceTitle = object?["price"] as? String {
cell.price.text = priceTitle
}
最佳答案
对于日期,使用 NSDateFormatter.stringFromDate 方法,因此您的代码应该是:
if let createdAt = object?["createdAt"] as? NSDate {
let dateFormatter = NSDateFormatter()
var theDateFormat = NSDateFormatterStyle.ShortStyle
dateFormatter.dateStyle = theDateFormat
cell.date.text = dateFormatter.stringFromDate(createdAt)
}
关于ios - 我如何从解析中获取数字数据和日期数据并将其放入 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30306415/