【问题标题】:Swift Sum multiple Int in Json data在 Json 数据中快速求和多个 Int
【发布时间】:2019-06-21 11:38:08
【问题描述】:

所以我有多个要求和的 json 数据,所以我尝试了循环,但它仍然不起作用得到结果

所以这是Json的数据:

    [

        {
        "projects_id": 13,
        "projects": "Immobi Track",
        "assignee_id": "193",
        "assignee": "Ivander",
        "counts": 8
    },
    {
        "projects_id": 13,
        "projects": "Immobi Track",
        "assignee_id": "195",
        "assignee": "Adrian ",
        "counts": 3
    },
    {
        "projects_id": 13,
        "projects": "Immobi Track",
        "assignee_id": "204",
        "assignee": "khalid",
        "counts": 11
    },
    {
        "projects_id": 13,
        "projects": "Immobi Track",
        "assignee_id": "164",
        "assignee": "Testing",
        "counts": 0
    },
    {
        "projects_id": 13,
        "projects": "Immobi Track",
        "assignee_id": "171",
        "assignee": "Ahma",
        "counts": 0
    },
    {
        "projects_id": 13,
        "projects": "Immobi Track",
        "assignee_id": "13",
        "assignee": "Hafi",
        "counts": 0
    },
    {
        "projects_id": 13,
        "projects": "Immobi Track",
        "assignee_id": "17",
        "assignee": "Bob ",
        "counts": 0
    },
    {
        "projects_id": 13,
        "projects": "Immobi Track",
        "assignee_id": "10",
        "assignee": "Hest",
        "counts": 0
    }
]

我尝试对每个受让人数据的“计数”数据求和,但我无法弄清楚,因为当我尝试打印它看起来像是不在数组内的单个数据但我只是将所有这些数据放入进入一个数组.....这是我的代码

          struct pivotProjectSum : Codable {

            let projects_id: Int
            let projects : String
            let assignee_id : String
            let assignee : String
            let counts : Int
            }


             private var pivot : [pivotProjectSum] = []


                 URLSession.shared.dataTask(with: JsonUrl) { (data, response, error) in
            guard let data = data else {return}

            do{
                let parsing = try JSONDecoder().decode([pivotProjectSum].self, from: data)
                self.pivot = parsing

                DispatchQueue.main.async {
                    if parsing.isEmpty{
                        let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
                        let alert = UIAlertController(title: "Data Empty", message: "No Data...", preferredStyle: .alert)
                        alert.addAction(action)
                        self.present(alert, animated: true, completion:  nil)
                    }else{
                        self.pivotTableView.reloadData()
                    }
                }
            }catch{
                print("This is the error????????‍♂️\(error)")
            }
        }.resume()
    }
}

            extension PicSummaryViewController : UITableViewDelegate,   UITableViewDataSource {
             func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
              return pivot.count
    }

               func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
              let piv = pivot[indexPath.row]
            //let pivots = pivot[indexPath.self]
             guard let cell =  pivotTableView.dequeueReusableCell(withIdentifier: "Pivot") as?  PicSummaryTableViewCell else {return UITableViewCell()}

        cell.UserLbl.text = piv.assignee
        cell.openLbl.text = "\(piv.counts)"
        projectLbl.text = piv.projects


        print(piv.counts)



                 let numbers = [piv.counts]
        var sum = 0
        var counter = 0

        print("This is the numbers????????‍♂️\(numbers)")

        while counter < numbers.count{

            var newValue = numbers[counter]
            sum += newValue
            counter += 1
        }

        print("this is the result ????\(counter)")

        let total = numbers.reduce(0, +)
        print("this is the result ????\(total)")

        for i in 0...numbers.count{
            sum += i
        }

        print("this is the result ????\(sum)")

        return cell

    }

   //i try every single method, but i still cant get the result. 

   // and no im not combine all of that sum method, i use it 1 method at a time

每个方法的结果都是这样的:

This is the numbers????????‍♂️[0]
this is the result ????1
0
This is the numbers????????‍♂️[0]
this is the result ????1
0
This is the numbers????????‍♂️[0]
this is the result ????1
8
This is the numbers????????‍♂️[8]
this is the result ????1
3
This is the numbers????????‍♂️[3]
this is the result ????1
11
This is the numbers????????‍♂️[11]
this is the result ????1
0
This is the numbers????????‍♂️[0]
this is the result ????1
0
This is the numbers????????‍♂️[0]
this is the result ????1
0
This is the numbers????????‍♂️[0]
this is the result ????1
0
This is the numbers????????‍♂️[0]
this is the result ????1

那么先生有什么帮助吗?我还是新手,在这个 Swift 开发者中学习

【问题讨论】:

    标签: json swift api sum


    【解决方案1】:

    你可以试试

     self.pivot = try JSONDecoder().decode([PivotProjectSum].self, from: data)
     let sum = self.pivot.map{ $0.counts }.reduce(0,+)
     print(sum)
    

    【讨论】:

      【解决方案2】:

      试试这个;

      let sum = pivot.map({ $0.counts }).reduce(0, +)
      

      【讨论】:

        猜你喜欢
        • 2019-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多