0

我有一些变量要传递给 ContentView。

这是 Data.swift 文件,其中包含获取变量的类和函数:

import SwiftUI

class Api {
    
    func getValues() {
        
        let url = URL(string: "URL")
        guard let requestUrl = url else { fatalError() }

        var request = URLRequest(url: requestUrl)
        request.httpMethod = "GET"
        request.setValue("User Agent", forHTTPHeaderField: "User-Agent")
        request.setValue("Auth", forHTTPHeaderField: "Authorization")

        URLSession.shared.dataTask(with: request) { (data, _, _) in
            
            let dataString = String(data: data!, encoding: .utf8)
  
            func convertToDictionary(text: String) -> [String: Any]? {
                if let data = text.data(using: .utf8) {
                    do {
                        return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
                    } catch {
                        print(error.localizedDescription)
                    }
                }
                return nil
            }
          
            let dict = convertToDictionary(text: dataString!)
            let array = dict!["array1"] as? NSArray
            let array0 = array![0] as? NSDictionary
            let id = array0!["id"] as! String
            let objects = array0!["object"] as! NSDictionary
            var total = 0
            var i = 0
            for object in objects {
                let key = object.key as! String
                let tmpDict = objects[(key)] as! NSDictionary
                let tmpStr = tmpDict["raw_value"] as! String
                let tmpD = Double(tmpStr)
                let value = Int(tmpD!)
                print(value)
                total+=value
                i+=1
                }
            print("ID: \(id)")
            let fractionalProgress = CGFloat(total / i)/6
            let roundedProgress = CGFloat(round(100*fractionalProgress)/100)
            print("Average value: \(roundedProgress)")
        }
        .resume()
    }
}

为了简单起见,我在下面包含了我正在使用的 JSON 的一部分:

{
  "array1": [
    {
      "link_id": 1,
      "object": {
        "2000000000007522336": {
          "is_draft": 0,
          "value": "4",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "4.00",
          "numerical_value": null
        },
        "2000000000016619051": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        }
      },
      "deleted_at": null,
      "id": "1_20",
      "status_id": 1,
      "target_num": 1
    },
    {
      "link_id": 55,
      "object": {
        "2000000000010046787": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000005323660": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000006311217": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000011929948": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000004856132": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000007455406": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000009846642": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000016910392": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000014735487": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        }
      },
      "deleted_at": null,
      "id": "55_20",
      "status_id": 1,
      "target_num": 2
    },
    {
      "link_id": 2,
      "object": {
        "2000000000012123534": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000004686641": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000006918209": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000007310987": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        },
        "2000000000013890209": {
          "is_draft": 0,
          "value": "6",
          "show_text": null,
          "show_icon_mobile": null,
          "raw_value": "6.00",
          "numerical_value": null
        }
      },
      "deleted_at": null,
      "id": "2_20",
      "status_id": 1,
      "target_num": 3
    }
  ]
}

这是内容视图:

import SwiftUI

struct ContentView: View {
    @State var show = false
    var body: some View {
        Text("Hello, world!")
            .onAppear() {
                Api().getValues()
            }
        Text("Values should appear here.")
        Circle()
            .trim(from: show ? "roundedProgress CGFloat" : 0.99, to: 1)
            .stroke(Color.green, style: StrokeStyle(lineWidth: 7.5, lineCap: .round, lineJoin: .round))
            .rotationEffect(.degrees(90))
            .rotation3DEffect(Angle(degrees: 180), axis: (x: 1, y: 0, z: 0))
            .frame(width: 100, height: 100)
            .animation(.easeOut)
            .onAppear() {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
                    self.show.toggle()
                }
            }
        }
    }

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

理想情况下,我想根据 roundedProgress 浮点值修剪环形视图,并在 onAppear() 之后的文本操作中显示这些值。

我尝试将整个函数放在 onAppear() 操作中,但没有任何改变。在没有 onAppear() 的情况下放置代码会在我用于获取数据的 URLSession 操作上显示错误“无法生成表达式诊断;请提交错误报告”。

4

0 回答 0