это JSON, который я пытаюсь опубликовать, он имеет массив JSON с именем Menus и внутри него есть объекты меню
{
"Menus":[
{
"Id":"5ae03b8e90435721f465050c",
"Name":"testmenu2"
},
{
"Id":"5ae03f4490435721f465051b",
"Name":"menu1"
}
],
"InStock":true,
"Pricing":{
"Price":1000,
"SaleEnd":"2018-04-20",
"SaleStart":"2018-04-20",
"SalePrice":120
},
"Sku":"chickenkoththutest",
"PreparationTime":{
"MinCount":"0",
"MinTime":"0",
"AdditionalPerItem":"0"
},
"Name":"chickenkoththutest",
"BusinessId":"5adda52c90435519b0c31aff",
"Attributes":[
],
"Cuisines":[
],
"Description":"none"
}
и это метод, которому я следую, но все остальные детали передают за исключением массива объектов. Сначала я конвертирую объекты в словарь и добавляю их в массив. Публикация одного словарного объекта работает, но когда я добавляю их в массив и пытаюсь опубликовать, он не может быть опубликован. Есть ли способ разместить массив объектов с помощью Alamofire
func AddMenuItem(deviceToken:String,Name:String,Sku:String,BusinessId:String,Pricing:Pricing,Menus:[Menus],Cuisines:[Cuisines],Attributes:[Attributes],Description:String,Instock:Bool,PreparationTime:PreparationTime,_ completion: @escaping (_ sucsessStatus:String, _ id:String,_ error_type:String)->()) {
let headers = [
"Authorization": "Bearer " + deviceToken,
"Accept": "application/json"
]
var categories = [AnyObject]()
var attributeList = [AnyObject]()
var cuisineList = [AnyObject]()
var options = [AnyObject]()
for menu in Menus{
if(Menus.count != 0){
let Cat: NSMutableDictionary = NSMutableDictionary()
Cat.setValue(menu._Id, forKey: "Id")
Cat.setValue(menu.Name, forKey: "Name")
// let array = JSON(Cat)
categories.append(Cat)}
}
for cuisine in Cuisines{
if(Cuisines.count != 0){
let Cat: NSMutableDictionary = NSMutableDictionary()
Cat.setValue(cuisine._Id, forKey: "Id")
Cat.setValue(cuisine.Name, forKey: "Name")
// let array = JSON(Cat)
cuisineList.append(Cat)}
}
for attr in Attributes{
if(Attributes.count != 0){
let Cat: NSMutableDictionary = NSMutableDictionary()
Cat.setValue(attr._Id, forKey: "Id")
Cat.setValue(attr.Name, forKey: "Name")
// let array = JSON(Cat)
attributeList.append(Cat)}
}
for option in OptioList.options{
if(OptioList.options.count != 0){
let Cat: NSMutableDictionary = NSMutableDictionary()
Cat.setValue(option.price, forKey: "Price")
Cat.setValue(option.Desc, forKey: "Description")
Cat.setValue(option.isrequired, forKey: "IsRequired")
// let array = JSON(Cat)
options.append(Cat)}
}
let prepared: NSMutableDictionary = NSMutableDictionary()
prepared.setValue(PreparationTime.MinTime, forKey: "MinTime")
prepared.setValue(PreparationTime.MinCount, forKey: "MinCount")
prepared.setValue(PreparationTime.AdditionalPerItem, forKey: "AdditionalPerItem")
//let pre = JSON(prepared)
let pricingdata: NSMutableDictionary = NSMutableDictionary()
pricingdata.setValue(Pricing.Price, forKey: "Price")
pricingdata.setValue(Pricing.SalePrice, forKey: "SalePrice")
pricingdata.setValue(Pricing.SaleStart, forKey: "SaleStart")
pricingdata.setValue(Pricing.SaleEnd, forKey: "SaleEnd")
// let pricingd = JSON(pricingdata)
let param = [ "Name" : Name,
"BusinessId" : BusinessId,
"Sku" : Sku,
"Menus":categories,
"Cuisines":cuisineList,
"Attributes":attributeList,
"Description":Description,
"PreparationTime": prepared ,
"Pricing":pricingdata,
"Customizations":options
] as [String : Any]
self._webApiHelper.SendHttpRequest(params: param as AnyObject, header_obj: headers, url: "Vendor/Menu/Items/Add", http_method: .post) { (response_value, error) in
print(response_value)
if(error != "NO_INT"){
if(response_value["Message"].string! == "SUCCESS")
{
completion(response_value["Message"].string! ,response_value["Id"].string!,"")
}
else{
completion(response_value["Message"].string!,"" ,"")
}
}
else{
completion( "","",response_value["Message"].string!)
}
}//SendHttpRequest
}
это метод webapihelper
var BASE_URL : String = "http://portal.blueicon.lk/api/"
var TOKEN_URL: String = "http://portal.blueicon.lk/token"
func SendHttpRequest(params: AnyObject, header_obj: [String: String], url:String, http_method:HTTPMethod, completion: @escaping (_ response_value: JSON, _ error_type: String)->()) {
Alamofire.request(BASE_URL + url, method: http_method, parameters: (params as! [String : Any]) , encoding: URLEncoding.default, headers: header_obj).responseJSON { (response:DataResponse<Any>) in
switch(response.result) {
case .success( _ ):
if let jsonValue = response.result.value {
let json = JSON(jsonValue)
completion(json, "")
}
break
case .failure(_):
print(response.result.error!)
completion(JSON(response.result.value), "NO_INT")
//"The Internet connection appears to be offline."
break
}
}//Alamofire
}//SendHttpRequest