Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何使一个数组与来自REST服务的另一个数组一致?_Arrays_Web Services_Uitableview_For Loop_Swift3 - Fatal编程技术网

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

Arrays 如何使一个数组与来自REST服务的另一个数组一致?,arrays,web-services,uitableview,for-loop,swift3,Arrays,Web Services,Uitableview,For Loop,Swift3,我试图用REST服务中的元素创建一个TableView,它是一个优惠券列表,包含描述、标题、类别和图像。因此,我首先反序列化数据,将其放入数组中,然后将其转换为每个部分的特定数组,但我的循环不起作用,有人能帮我吗 这是我的代码: var couponsTitle : [String] = [] var couponsDesc : [String] = [] var couponsCat : [String] = [] func getCoupons(){

我试图用REST服务中的元素创建一个TableView,它是一个优惠券列表,包含描述、标题、类别和图像。因此,我首先反序列化数据,将其放入数组中,然后将其转换为每个部分的特定数组,但我的循环不起作用,有人能帮我吗

这是我的代码:

    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

}

我最大的问题是,我不知道如何将数组放入循环,但我指的是每个部分的规格,标题、描述、类别等。有什么想法吗?

与其为每个属性设置三个数组,为什么不为具有三个属性的优惠券设置一个自定义类

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
    }
}
如果您这样做,您可以通过这样做来避免如此多的循环

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

非常感谢你的回答,我需要这样的东西我只有一个问题,我必须如何声明couponsArray?因为我的代码是这样的:var couponsArray:[String]=[],它向我抛出了一个错误:无法将类型为“优惠券”的值转换为预期的参数类型“String”。您对此plz有什么线索吗?错误是告诉您无法将优惠券添加到字符串数组中。注释中的示例数组声明了一个字符串数组。您需要的是这样一个优惠券数组,var couponsArray:[优惠券]=[]。然后,您应该能够向数组中添加优惠券。好的,现在我在cellForIndexPath中遇到了一个问题,因为如果我将其声明为:cell.couponTitle.text=couponTitle[indexPath.row]会引发以下错误:“无法将类型为'couponTitle'的值分配给类型为'String?。。。所以我尝试过的解决方案是:cell.couponTitle.text=couponsttitle[indexPath.row]作为AnyObject as?字符串,但仍然不起作用。我怎样才能解决它?