Swift 将 Eureka 日期转换为字符串以供 firebase 存储
Swift converting Eureka date into String for firebase storing
我正在尝试从 Eureka DateTimeRow 收集日期值,然后将其存储到 Firebase 中,但要存储它,我需要它采用字符串格式。我已尝试进行此转换,但收到错误消息“无法将类型 'Foundation.Date' (0x108af27e8) 的值转换为 'Swift.String' (0x1086e99f8)。”
我想知道我的转换方法是否遗漏了什么。
日期时间行:
<<< DateTimeRow("startDate"){
[=11=].title = "Start Date"
[=11=].value = NSDate() as Date
[=11=].cellUpdate { (cell, row) in
cell.datePicker.minimumDate = Date()
}
[=11=].onChange { row in
start = row.value!
}
}
获取 Erueka 形式的值并进行转换的代码:
let valuesDictionary = form.values()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let formattedDate = formatter.date(from: valuesDictionary["startDate"] as! String)
谢谢大家的反馈。
如你所愿将从 a Date
(Eureka) 转换为 a String
(Firebase) , 你应该使用 string(from:)
method of the DateFormatter
, whereas you are attempting to use the date(from:)
方法。
// Date to String
func string(from date: Date) -> String
// String to Date
func date(from string: String) -> Date?
我正在尝试从 Eureka DateTimeRow 收集日期值,然后将其存储到 Firebase 中,但要存储它,我需要它采用字符串格式。我已尝试进行此转换,但收到错误消息“无法将类型 'Foundation.Date' (0x108af27e8) 的值转换为 'Swift.String' (0x1086e99f8)。” 我想知道我的转换方法是否遗漏了什么。
日期时间行:
<<< DateTimeRow("startDate"){
[=11=].title = "Start Date"
[=11=].value = NSDate() as Date
[=11=].cellUpdate { (cell, row) in
cell.datePicker.minimumDate = Date()
}
[=11=].onChange { row in
start = row.value!
}
}
获取 Erueka 形式的值并进行转换的代码:
let valuesDictionary = form.values()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let formattedDate = formatter.date(from: valuesDictionary["startDate"] as! String)
谢谢大家的反馈。
如你所愿将从 a Date
(Eureka) 转换为 a String
(Firebase) , 你应该使用 string(from:)
method of the DateFormatter
, whereas you are attempting to use the date(from:)
方法。
// Date to String func string(from date: Date) -> String // String to Date func date(from string: String) -> Date?