这里我需要获取特定数组的索引,其中键值对item.defaultShipping == "true"
然后我需要获取特定数组的索引并传入模型类,以便获取相应的数据它应该在另一个Api中传递,但是当我尝试下面的方法时,它显示错误Contexual type 'Any' cannot be used within dictionary literal
在let 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)
答案 0 :(得分:0)
从Swift 3开始,您可以使用enumerated()
,这样您就可以获得索引和值,如下所示:
for (index, item) in customerShippingAddressModel.enumerated() {
if item.defaultShipping == "true" {
// you can get the item and index value here.
}
}