Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum element cannot be referenced as an instance member

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
    }
}
}
like image 878
Suman Roy Avatar asked Apr 28 '17 10:04

Suman Roy


1 Answers

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.

like image 103
Suman Roy Avatar answered Oct 13 '22 00:10

Suman Roy