简体   繁体   中英

How to conform an array from another array that comes from a REST Service?

I'm trying to create a TableView with elements that comes from a REST Service, It's a list of coupons that has description, title, category and images. So I deserialize the data first, put it in an array and then convert it into an specific array per each section, but my loop is not working, can anyone help me please?

This is my code:

    var couponsTitle : [String] = []

    var couponsDesc : [String] = []

    var couponsCat : [String] = []



    func getCoupons(){

        let miURL = URL(string: RequestConstants.requestUrlBase)

        let request =  NSMutableURLRequest(url: miURL!)

        request.httpMethod = "GET"

        if let data = try? Data(contentsOf: miURL! as URL) {

            do {

                let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary

                let parseJSON = json

                let object = parseJSON?["object"] as! NSDictionary

                let mainCoupon = object["mainCoupon"] as! NSArray 

                let coupons = object["coupons"] as! NSArray



                for i in mainCoupon {
                    self.couponsCat.append((mainCoupon[i as! Int] as AnyObject).value(forKey: "category"))
                }

                for i in coupons {
                    self.couponsCat.append((coupons[i as! Int] as AnyObject).value(forKey: "category"))
                }

                for i in mainCoupon {

                    self.couponsDesc.append((mainCoupon[i as! Int] as AnyObject).value(forKey: “description"))

                }



                for i in coupons {

                    self.couponsDesc.append((coupons[i as! Int] as AnyObject).value(forKey: “description"))

                }

                for i in mainCoupon {

                    self.couponsTitle.append((mainCoupon[i as! Int] as AnyObject).value(forKey: “name"))

                }



                for i in coupons {

                    self.couponsTitle.append((coupons[i as! Int] as AnyObject).value(forKey: “name"))

                }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeTableViewCell

        cell.couponTitle.text = couponsTitle[indexPath.row]
        cell.couponDescription.text = couponsDesc[indexPath.row].    
        cell.couponCategory.text = couponsCat[indexPath.row]
        return cell

}

My biggest issue is that I don't know how to put in a loop the array but with the specification of each section (I mean, the title, description, category, etc.) Any idea?

Rather than having three arrays (one for each property), why not have a custom class for Coupon that has three properties?

class Coupon: AnyObject {

    var description: String
    var title: String
    var category: String

    init(description: String, title: String, category: String) {
        self.description = description
        self.title = title
        self.category = category
    }
}

If you do it that way, you can avoid so many loops by doing something like this

for coupon in mainCoupon {

            let description = mainCoupon["description"]
            let title = mainCoupon["name"]
            let category = mainCoupon["category"]

            let newCoupon = Coupon(description: description, title: title, category: category)

            couponsArray.append(newCoupon)
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM