Answers for "send array of dictionaries in alamofire requests"

1

Sending array of dictionaries with alamofire

// The problem was in append method. I have coded on PHP 5 years and forgoted that in Swift the indexes not automatically assigned like in PHP. So, my first bugged code was:

func getParameters() -> [[String: AnyObject]] {
    var result = [[String: AnyObject]]()

    for mmap in mmaps {
        let material: [String: AnyObject] = [
            "material_id": mmap.material.id,
            "quantity": mmap.qty
        ]
        result.append(material)
    }

    return result
}

// The answer is hard assign the keys as you need:

func getParameters() -> [String: [String: AnyObject]] {
    var result = [String: [String: AnyObject]]()

    let mmaps = self.mmaps.allObjects as [Mmap]
    for i in 0..<mmaps.count {
        let mmap = mmaps[i]
        let material: [String: AnyObject] = [
            "material_id": mmap.material.id,
            "quantity": mmap.qty
        ]
        result["\(i)"] = material
    }

    return result
}
Posted by: Guest on August-04-2020

Code answers related to "send array of dictionaries in alamofire requests"

Code answers related to "Swift"

Browse Popular Code Answers by Language