swift 中无法使用 Moya 解析字符串以外的变量
Not able to parse variable other than string using Moya in swift
我正在 Swift 进行 iOS 项目。我使用 Moya 框架进行 API 处理和解析。它工作得很好。但是当我尝试解析除字符串以外的变量时,它显示错误:
"Missing argument for parameter 'transformation' in call"
这是我的映射器class
import Mapper
class MyMapperClaa:Mappable {
var dateVariable: NSDate?
required init(map: Mapper) throws{
try dateVariable = map.from("date")
}
}
抱歉,您正在使用这个库:https://github.com/lyft/mapper。来自那里的例子:
private func extractDate(object: Any?) throws -> Date {
guard let rawDate = object as? String else {
throw MapperError.convertibleError(value: object, type: Date.self)
}
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "your date format"
if let date = dateFormatter.date(from: rawDate) {
return date
} else {
throw MapperError.convertibleError(value: object, type: Date.self)
}
}
struct DateModel: Mappable {
let date: Date
init(map: Mapper) throws {
try date = map.from("date", transformation: extractDate)
}
}
为 Date 创建了一个扩展,它对我有用
extension Date:Convertible
{
public static func fromMap(_ value: Any) throws -> Date {
guard let rawDate = value as? String else {
throw MapperError.convertibleError(value: value, type: Date.self)
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
if let date = dateFormatter.date(from: rawDate) {
return date
} else {
throw MapperError.convertibleError(value: value, type: Date.self)
}
}
}
我正在 Swift 进行 iOS 项目。我使用 Moya 框架进行 API 处理和解析。它工作得很好。但是当我尝试解析除字符串以外的变量时,它显示错误:
"Missing argument for parameter 'transformation' in call"
这是我的映射器class
import Mapper
class MyMapperClaa:Mappable {
var dateVariable: NSDate?
required init(map: Mapper) throws{
try dateVariable = map.from("date")
}
}
抱歉,您正在使用这个库:https://github.com/lyft/mapper。来自那里的例子:
private func extractDate(object: Any?) throws -> Date {
guard let rawDate = object as? String else {
throw MapperError.convertibleError(value: object, type: Date.self)
}
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "your date format"
if let date = dateFormatter.date(from: rawDate) {
return date
} else {
throw MapperError.convertibleError(value: object, type: Date.self)
}
}
struct DateModel: Mappable {
let date: Date
init(map: Mapper) throws {
try date = map.from("date", transformation: extractDate)
}
}
为 Date 创建了一个扩展,它对我有用
extension Date:Convertible
{
public static func fromMap(_ value: Any) throws -> Date {
guard let rawDate = value as? String else {
throw MapperError.convertibleError(value: value, type: Date.self)
}
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
if let date = dateFormatter.date(from: rawDate) {
return date
} else {
throw MapperError.convertibleError(value: value, type: Date.self)
}
}
}