gpt4 book ai didi

swift - 将日期从 Swift 发送到 Vapor 3

转载 作者:行者123 更新时间:2023-12-04 08:06:46 27 4
gpt4 key购买 nike

我无法从 iOS Swift 5 向 Vapor 3 发送日期。这是我从 Vapor 得到的错误:

POST /admin/test Expected date string to be ISO8601-formatted.

这是我在 iOS 中创建日期的方法
let today = Date()
let f = ISO8601DateFormatter()
f.formatOptions.insert(.withFractionalSeconds)
let finalDate = f.string(from: today)
这是我在 iOS 中的交易模型
final class Transaction: Codable, ReflectedStringConvertible {

var id: Int?
var date: String
var amount: Int
var planeID: Int
var userID: UUID
var month: Int
var year: Int

init(date: String, amount: Int, planeID: Int, userID: UUID, month: Int, year: Int) {
self.date = date
self.amount = amount
self.planeID = planeID
self.userID = userID
self.month = month
self.year = year
}
}
这是我的 Steam API

func createHandler (_ req: Request, transaction: Transaction) throws -> Future<Transaction> {
return transaction.save(on: req)
}

这是我在 Vapor 方面的交易模型

final class Transaction: Codable {

var id: Int?
var date: Date
var amount: Int
var planeID: Plane.ID
var userID: User.ID
var month: Int
var year: Int
//fixes Swift 5 bug
typealias Database = PostgreSQLDatabase


init(date: Date, amount: Int, planeID: Plane.ID, userID: User.ID, month: Int = 0, year: Int = 0) {
self.date = date
self.amount = amount
self.planeID = planeID
self.userID = userID
self.month = month
self.year = year
}
}

任何想法我做错了什么?是不是我发送的是日期字符串而不是日期?非常感谢。

最佳答案

答案是删除小数秒

let today = Date()
let f = ISO8601DateFormatter()
let finalDate = f.string(from: today)

关于swift - 将日期从 Swift 发送到 Vapor 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66189828/

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