Enum element cannot be referenced as an instance member

Suman Roy picture Suman Roy · Apr 28, 2017 · Viewed 7.6k times · Source

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

Answer

Suman Roy picture Suman Roy · Apr 30, 2017

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.