I have a computed property of type Parameters in my APIRouter
// MARK: - Parameters private var parameters: Parameters? { switch self { case .searchForDoctors(let doctorsFilter): var params: Parameters = ["main_category_id": doctorsFilter.0, "page": doctorsFilter.1, "specialty_id": doctorsFilter.2, "city_id": doctorsFilter.3, "region_id": doctorsFilter.4, "name": doctorsFilter.5, "company_id": doctorsFilter.6, "order_by": doctorsFilter.7] return params default: return nil } }
some values in the Typealias called doctorsFilter are optional. currently I have a warning asking me to provide default value for the optional values, and I don't want to provide default values , I want to check if the value exist to add it, otherwise i will not add the key and the value
how can I safely unwrap the optional values and add it to the parameters dictionary with out saying if let for all optional values? example:
if let specialtyID = doctorsFilter.2 { params["specialty_id"] = specialtyID }
I don't want to unwrap it this way as I will check for all optional values and it will take more lines of code
EDIT:- the DoctorsFilter type is documented, when I initialize an instance of type DoctorsFilter the autocomplete tells me which of them is what, I I've thought about making the DoctorsFilter class before but I'm looking for another way if any, maybe a built in reserved word can handle the whole situation! , I want to make it simple as much as possible. making a function that handles the dictionary and returns it in DoctorsFilter class is an option. I'm thinking of adding this function to the APIRouter, is it fine to add it there? is it the rule of the APIRouter to handle the parameters ? or the APIRouter just interested in taking the parameters and will not handle it ?
https://stackoverflow.com/questions/65385823/safely-unwrapping-optional-values-and-add-it-to-alamofire-parameters December 21, 2020 at 07:25AM
没有评论:
发表评论