通过 Alamofire Object Mapper Swift 从映射数组中获取值
Posted
技术标签:
【中文标题】通过 Alamofire Object Mapper Swift 从映射数组中获取值【英文标题】:Get value from a mapped array by Alamofire Object Mapper Swift 【发布时间】:2017-10-30 06:45:25 【问题描述】:下面是我使用 Object Mapper 映射 JSON 的代码,我成功过滤了数组。
Alamofire.request("https://project-isdental-cammy92.c9users.io/api/v2.0/company/1", method: .get, parameters: nil, encoding: URLEncoding.default, headers: params).responseObject (response: DataResponse<ProductGroup>) in
let ProductGroupRes = response.result.value
if let Product = ProductGroupRes?.products
self.collectionArray = Product.filter($0.group_type == 1) as NSArray
print(self.collectionArray)
self.tableArray = Product.filter($0.group_type == 2) as NSArray
这是我过滤后的数组,即collectionArray
"IndiaSupply.Product(group_title: Optional(\"Recommended\"),
group_type: Optional(1),
products: Optional([IndiaSupply.ProductArray(
product_id: Optional(1),
product_name: Optional(\"Product 1\"),
product_price: Optional(\"Rs 1,999/-\"),
product_category: Optional(\"Surgical\"),
product_image: Optional(\"\"),
product_description: Optional(\"Description 1\")),
IndiaSupply.ProductArray(product_id: Optional(2),
product_name: Optional(\"Product 2\"),
product_price: Optional(\"Rs 1,999/-\"),
product_category: Optional(\"Surgical\"),
product_image: Optional(\"\"),
product_description: Optional(\"Description 1\")),
IndiaSupply.ProductArray(product_id: Optional(3),
product_name: Optional(\"Product 3\"),
product_price: Optional(\"Rs 1,999/-\"),
product_category: Optional(\"Surgical\"),
product_image: Optional(\"\"),
product_description: Optional(\"Description 1\")),
IndiaSupply.ProductArray(product_id: Optional(4),
product_name: Optional(\"Product 4\"),
product_price: Optional(\"Rs 1,999/-\"),
product_category: Optional(\"Surgical\"),
product_image: Optional(\"\"),
product_description: Optional(\"Description 1\")),
)
如何从该结果中获取产品数组,然后从中获取价值。我无法使用此代码获取它:
let Pro = self.collectionArray.value(forKey : "products") as NSArray
【问题讨论】:
请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。 这次我投了反对票,因为I have asked this before。 【参考方案1】:您成功映射它太棒了,现在您只需使用这些模型对象,例如,
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
//....
let aProduct = self.collectionArray[indexPath.item] as! Product
cell.labelName.text = aProduct.product_name
//and so on
【讨论】:
没有还是不能访问product_name,我只能访问group_title和group_type 能否请您与我分享您的代码,以便我可以更快地查看它。 分享您的电子邮件? @Baig:虽然社区不能阻止帮助者分享他们的私人联系方式,如果他们真的愿意的话,我们希望它不要发生。如果所有帮助都可以公开完成(并显示),社区就会受益。谢谢!以上是关于通过 Alamofire Object Mapper Swift 从映射数组中获取值的主要内容,如果未能解决你的问题,请参考以下文章
当我在 Alamofire 中使用 UserDefaults.standard.object 时,它显示“调用中的额外参数”错误