Enum element cannot be referenced as an instance member

I am creating an API layer using Moya and keep getting the above error for the target .updateMyWeightGoal

when I make a request for the endpoint.

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

      

I created another Moya API of the same type and call it using the same goalAPI

one and it works fine.

Any ideas that might be causing this problem

For reference: class definition for type 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"]
}
}

      

And the API definition:

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

      

+5


source to share


3 answers


This is the dumbest thing.



As it turns out, the error occurs, not from the enumeration, but from the success block. It was expecting an object of type Mappable, which I did not provide.

+17


source


You mean .updateMyWeightGoal

as an instance member ( .

) when it is declared as an enum. Try to change:

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

      



For

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

      

+2


source


There was the same mistake. In my case, the problem was that I typed:

if someVariable = .AnEnumValue

      

I meant the following:

if someVariable == .AnEnumValue

      

Main difference: =

versus ==

.

Not sure how the compiler got to this particular error message, but it fixed the problem.

0


source







All Articles