我正在使用Moya创建API层,并在创建端点请求时继续为.updateMyWeightGoal
目标获取上述错误。
goalAPI.request(target: .updateMyWeightGoal(weightGoalData: goalInfo), success: { (response) in
//
}){ (response: [String : Any]) in
print(response)
}
我已经创建了另一个相同类型的Moya API,并使用与此相同的goalAPI
来调用它,并且它工作正常。
任何可能导致此问题的想法
此处的参考是weightGoalData
类型
class UpdatedWeightGoalInfo: Mappable {
var consumerUserID: Int?
var height: String?
var weight: String?
var goalWeight: String?
init() {
}
convenience init(userId: Int, weightGoalData: WeightGoalResponse) {
self.init()
consumerUserID = userId
height = "\(weightGoalData.currentHeight)"
weight = "\(weightGoalData.currentWeight)"
goalWeight = "\(weightGoalData.goalWeight)"
}
required init?(map: Map) {
}
func mapping(map: Map) {
consumerUserID <- map["consumerUserId"]
height <- map["height"]
weight <- map["weight"]
goalWeight <- map["goalWeight"]
}
}
API的定义:
enum GoalSettingAPI: AccessTokenAuthorizable {
case updateMyWeightGoal(weightGoalData: UpdatedWeightGoalInfo)
}
extension GoalSettingAPI: TargetType {
var parameterEncoding: ParameterEncoding {
switch self {
default:
return JSONEncoding.default
}
}
var baseURL: URL { return URL(string: appBaseURL + "*hidden*/")! }
var path: String {
switch self {
case .updateMyWeightGoal(_):
return "updateMyWeightGoal"
}
}
var method: Moya.Method {
switch self {
case .updateMyWeightGoal(_):
return .post
}
}
var parameters: [String: Any]? {
switch self {
case .updateMyWeightGoal(let weightGoalData):
return weightGoalData.toJSON()
}
}
var sampleData: Data {
switch self {
default:
return Data()
}
}
var task: Task {
switch self {
default:
return .request
}
}
var shouldAuthorize: Bool {
switch self {
default:
return false
}
}
}
答案 0 :(得分:26)
这是最愚蠢的事情。
事实证明错误来了,不是来自枚举,而是来自成功块。它期待一个Mappable类型的对象,我没有提供。
答案 1 :(得分:3)
当您将.updateMyWeightGoal
声明为枚举时,您将其称为实例成员(.
)。尝试更改:
goalAPI.request(target: .updateMyWeightGoal(weightGoalData: goalInfo)
要
goalAPI.request(target: GoalSettingAPI.updateMyWeightGoal(weightGoalData: goalInfo)
答案 2 :(得分:1)
有相同的错误。就我而言,问题是我输入了以下内容:
if someVariable = .AnEnumValue
我的意思是:
if someVariable == .AnEnumValue
关键区别在于=
与==
。
不确定编译器如何处理此特定错误消息,但这可以解决问题。