12

I'm creating an API layer using Moya and keep getting the above mentioned error for the .updateMyWeightGoal target when I'm creating a request for an endpoint.

    goalAPI.request(target:  .updateMyWeightGoal(weightGoalData: goalInfo),  success: { (response) in
        //
    }){ (response: [String : Any]) in
        print(response)
    }

I've created another Moya API of the same type and am calling it using the same goalAPI as this and it's working fine.

Any ideas what might be causing this issue

For reference here is the class definition for the weightGoalData type

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"]
}
}

And the definition of the 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
    }
}
}
Suman Roy
  • 673
  • 5
  • 18
  • 1
    Please show where `.updateMyWeightGoal` and `goalAPI.request` is defined. – Oskar Apr 28 '17 at 10:48
  • @Oskar: Okay, added those in. And goalAPI.request is working fine as I'm using another API from the same group which is not giving me any trouble – Suman Roy Apr 28 '17 at 10:53

3 Answers3

33

This is the stupidest thing.

As it turns out the error was coming, not from the enum, but from the success block. It was expecting an object of type Mappable, which I wasn't providing.

Suman Roy
  • 673
  • 5
  • 18
  • 3
    Similar thing just happened to me, I got this error in a ternary expression but it was actually because I forgot to finish the comparison in the first part (`int ? .foo : .bar` instead of `int > 0 ? .foo : .bar`). Much as I like Swift, its compiler messages still leave a lot to be desired. – John Montgomery May 03 '18 at 22:30
  • Yeah, Xcode in recent years is less and less helpful with his error/warning messages... – Soberman May 09 '18 at 11:19
  • Yea a similar thing happened to me. I was trying to use the tertiary operator to set a state value to an enum case, but my conditional value couldn't actually resolve to true or false, so it gave the enum error as you described. Not entirely helpful. – Acadian_Ghost Jul 19 '19 at 13:41
  • I forgot to put dots before enum cases. Like "case one:" instead of "case .one:" – Roman Dec 02 '21 at 12:56
5

You're referring to .updateMyWeightGoal as an instance member (.) when it is declared as an enum. Try changing:

goalAPI.request(target: .updateMyWeightGoal(weightGoalData: goalInfo)

To

goalAPI.request(target: GoalSettingAPI.updateMyWeightGoal(weightGoalData: goalInfo)
Oskar
  • 3,625
  • 2
  • 29
  • 37
1

Had the same error. In my case the issue was that I had typed:

if someVariable = .AnEnumValue

What I meant was:

if someVariable == .AnEnumValue

The key difference being = vs ==.

Not sure how the compiler landed on this particular error message, but this fixed the problem.

Tim
  • 14,447
  • 6
  • 40
  • 63