这里我有一个集合视图,其中数据将从模型类传递到这里我需要从special_price
获取CustomAttribute
键值对,并且需要传递集合视图,其中只有一些产品将有特价和一些产品没有特价如何将数据传递到集合视图我无法实现它可以任何人帮我如何传递数据新到swift 3如果有任何错误的声明或写模型类请纠正我?
var listClassModel : ModelClass?
var items = [List]()
func listCategoryDownloadJsonWithURL(listUrl: String) {
print(listUrl)
let url = URL(string: listUrl)!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil { print(error!); return }
do {
if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [String:AnyObject] {
let objArr = jsonObj["items"] as? [[String:Any]]
let objAdd = objArr!.map{List(dict: $0)}
self.items.append(contentsOf: objAdd)
self.listClassModel = ModelClass(dict: jsonObj as [String : AnyObject])
DispatchQueue.main.async {
guard let obj = self.listClassModel else { return }
let itemsCount = obj.items.count
print(itemsCount)
for i in 0..<itemsCount {
let customAttribute = obj.items[i].customAttribute
print(customAttribute.count)
for j in 0..<customAttribute.count {
if customAttribute[j].attributeCode == "image" {
let baseUrl = "http://192.168.1.11/magento2/pub/media/catalog/product"
self.listCategoryImageArray.append(baseUrl + customAttribute[j].value)
}
if customAttribute[j].attributeCode == "special_price" {
self.specialPrice.append(customAttribute[j].value)
}
}
}
print(self.specialPrice)
self.activityIndicator.stopAnimating()
self.activityIndicator.hidesWhenStopped = true
self.collectionView.reloadData()
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.isHidden = false
self.tableView.reloadData()
}
}
} catch {
print(error)
}
}
task.resume()
}
这是我的模型类
struct ModelClass {
var items : [List]
var totalCount : Int
var searchCriteria : SearchCriteria
init(dict:[String:AnyObject]) {
totalCount = dict["total_count"] as! Int
let searchDict = dict["search_criteria"] as? [String:AnyObject]
searchCriteria = SearchCriteria(dict: searchDict!)
let arr = dict["items"] as? [[String:AnyObject]]
var listArr = [List]()
for obj in arr! {
listArr.append(List(dict: obj))
}
items = listArr
}
}
struct List {
let name : String
let sku : Any
let id : Int
let attributeSetId : Int
let price : Int
let status : Int
let visibility : Int
let typeId: String
let createdAt : Any
let updatedAt : Any
var customAttribute = [ListAttribute]()
init(dict : [String:Any]) {
if let customAttribute = dict["custom_attributes"] as? [[String: AnyObject]] {
var result = [ListAttribute]()
for obj in customAttribute {
result.append(ListAttribute(json: obj)!)
}
self.customAttribute = result
} else {
self.customAttribute = [ListAttribute]()
}
self.name = (dict["name"] as? String)!
self.sku = dict["sku"]!
self.id = (dict["id"] as? Int)!
self.attributeSetId = (dict["attribute_set_id"] as? Int)!
self.price = (dict["price"] as? Int)!
self.status = (dict["status"] as? Int)!
self.visibility = (dict["visibility"] as? Int)!
self.typeId = (dict["type_id"] as? String)!
self.createdAt = dict["created_at"]!
self.updatedAt = dict["updated_at"]!
}
}
struct ListAttribute {
let attributeCode : String
let value : String
init?(json : [String:AnyObject]) {
self.attributeCode = (json["attribute_code"])! as! String
self.value = (json["value"])! as! String
}
}
这是我的形象,我的Json看起来如何