简体   繁体   English

如何使一个来自REST服务的另一个数组与一个数组一致?

[英]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. 我正在尝试使用来自REST服务的元素创建TableView,这是一张包含说明,标题,类别和图像的优惠券列表。 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? 而不是拥有三个数组(每个属性一个),为什么不为Coupon提供一个具有三个属性的自定义类?

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)
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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