Search code examples
iosswiftmoya

Optional parameters in Moya task


I am using Moya and have a situation where some parameters of a request to an endpoint are not required, so the target looks like this:

case save(projectId: Int?, seconds: Int, description: String?)

Currently Moya's requestParameters(parameters:encoding:) method expects a dictionary of type [String:Any].

Is there a workaround for this scenario? Thanks in advance.


Solution

  • Tigran jan if-let ov stugi ete nil chi, nor qic dict-i mej.

    switch self {
    case .save(projectId: let projectId, seconds: let seconds, description: let description): 
        var params: [String:Any] = ["seconds" : seconds] 
        if let projectId = projectId 
            params["projectId"] = projectId
        } 
    
        if let description = description {
            params["description"] = description
        }
        return .requestParameters(parameters: params, encoding: JSONEncoding.default)
    }