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
    }
}
}
2
  • 1
    Please show where .updateMyWeightGoal and goalAPI.request is defined.
    – Oskar
    Apr 28, 2017 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, 2017 at 10:53

3 Answers 3

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.

4
  • 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. May 3, 2018 at 22:30
  • Yeah, Xcode in recent years is less and less helpful with his error/warning messages...
    – Soberman
    May 9, 2018 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. Jul 19, 2019 at 13:41
  • I forgot to put dots before enum cases. Like "case one:" instead of "case .one:"
    – Roman
    Dec 2, 2021 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)
4
  • Doing this is throwing Ambiguous reference to member 'print(_:separator:terminator:)' for the print statement in the failure block.
    – Suman Roy
    Apr 28, 2017 at 11:19
  • 1
    Change it to Swift.print.
    – Oskar
    Apr 28, 2017 at 11:40
  • Thanks for the help, but I just found the issue.
    – Suman Roy
    Apr 30, 2017 at 5:41
  • 1
    @Oskar Thanks! you made my day!
    – Tommy
    Dec 21, 2020 at 10:03
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.