Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save waseem-pk/5b6525bb5abea8e452afb32d47b72e64 to your computer and use it in GitHub Desktop.
Save waseem-pk/5b6525bb5abea8e452afb32d47b72e64 to your computer and use it in GitHub Desktop.
To use Dictionary in the parameter for Alamofire Request, Swift iOS
//First to decalre Parameter
var array = [[String : Any]]()
//MARK:- to call Api
func postRequest_toServer(){
let apiURL = AppConfig.shared.apiBasicURl + "user/post-new-job"
let objPostNewJob = PostNewJob()
let serverParameters : [String : Any] = [
"user_id" : AppConfig.shared.retriveUserData().user_id ,
"booking_type" : "2021-11-25" ,
"bookdatetime" : "12:49:22",
"latitude" : "33.3332332",
"longitude" : "71.232323",
"order_detail" : self.array
}
print( "\(serverParameters) \n" )
self.activityIndicator.startAnimation()
Alamofire.request(apiURL, method: .post, parameters: serverParameters , encoding: JSONEncoding.default)
.responseJSON { response in
if let status = response.response?.statusCode {
let json : JSON = JSON(response.result.value)
if status > 199 && status < 300 {
// self.dismiss(animated: true)
print("In true block")
let resultStatus = json["status"].intValue
if resultStatus == 1 {
// result ok
}else{
// reulst false
}
}else{
self.activityIndicator.stopAnimation()
print(" Some Error : ")
self.showAlert(userMessage: "Bad request.")
}
}}
} // end of function
// How to fill the Array
func fillarray(){
for row in arraySelectedVehicle {
let objPostedOrder = OrderDetail()
objPostedOrder.adon_id = row.adoninfo?.first?.id ?? 0
objPostedOrder.category_id = row.id
objPostedOrder.no_adon = row.no_adon
objPostedOrder.no_category = row.no_category
self.array.append(objPostedOrder.getDictFormat())
}
//
}
// Just a declareion of the object
class OrderDetail : Mappable {
required init?(map: Map) {
}
func mapping(map: Map) {
}
var category_id : Int = 0
var adon_id : Int = 0
var no_category : Int = 0
var no_adon : Int = 0
init() {
self.category_id = 0
self.adon_id = 0
self.no_adon = 0
self.no_category = 0
}
init(category_id : Int,adon_id : Int , no_category : Int ,no_adon : Int ) {
self.category_id = category_id
self.adon_id = adon_id
self.no_adon = no_adon
self.no_category = no_category
}
func toJSON() -> [String:Any] {
var dictionary: [String : Any] = [:]
dictionary["category_id"] = self.category_id
dictionary["adon_id"] = self.adon_id
dictionary["no_category"] = self.no_category
dictionary["no_adon"] = self.no_adon
return dictionary
}
func getDictFormat() -> [String: Any]{
return [ "category_id" : category_id, "adon_id" : adon_id, "no_category" : no_category, "no_adon" : no_adon]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment