如何获取模型类的数组索引在swift 3中的另一个api中传递相应的数据?

时间:2017-12-01 07:12:40

标签: ios swift3

这里我需要获取特定数组的索引,其中键值对item.defaultShipping == "true"然后我需要获取特定数组的索引并传入模型类,以便获取相应的数据它应该在另一个Api中传递,但是当我尝试下面的方法时,它显示错误Contexual type 'Any' cannot be used within dictionary literallet parameters下面的任何人都可以帮助我解决这个问题?

这是我的代码

        var i = 0
        for item in customerShippingAddressModel {
            if item.defaultShipping == "true" {

            }
            i += 1
        }
        let arr = customerShippingAddressModel[i]
        let parameters : [String: Any] = ["address":
            [ "region": "\(arr.region.region)",
                "region_code": "\(arr.region.regionCode)",
                "region_id": "\(arr.region.regionId)",
                "country_id": "\(arr.countryId)",
                "company": "\(arr.company)",
                "telephone": "\(arr.telephone)",
                "postcode": "\(arr.postCode)",
                "city": "\(arr.city)",
                "firstname": "\(arr.firstName)",
                "lastname": "\(arr.lastName)",
                "email": "\(arr.email)",
                "prefix": "",
                "sameAsBilling": 1,
                "street": ["0": "\((arr.customerStreet[0])!)",
                    "1": "\((arr.customerStreet[1])!)"]]]
        print(parameters)

1 个答案:

答案 0 :(得分:0)

从Swift 3开始,您可以使用enumerated(),这样您就可以获得索引和值,如下所示:

for (index, item) in customerShippingAddressModel.enumerated() {
  if item.defaultShipping == "true" {
    // you can get the item and index value here.
  }
}