代码之家  ›  专栏  ›  技术社区  ›  Anilkumar iOS Developer

线程8:信号sigabret

  •  0
  • Anilkumar iOS Developer  · 技术社区  · 6 年前

    我正在尝试用Swift解析我的api响应。 但是,我有以下错误

    线程8:信号sigabret

    我的json模态类是

    struct storyDataClass : Codable {
        var storyDictionary :  storyDictionary?
    }
    
    struct storyDictionary: Codable {
        var nameString : String?
        var lineString : String?
        var image : String?
    }
    

    而API调用是

        dataViewModelClass.getData(completionHandler: {data, error -> Void in
            if (data != nil) {
    
                print(data?.count)
    
                if let retriveData = data {
    
                    for (index,value) in retriveData.enumerated() {
                        print(index)
                      //  print(value)
    
                        var dataJson = storyDataClass.init(storyDictionary: value as! storyDictionary)
    
                        print(dataJson.storyDictionary?.lineString)
    
                    }
                }
    
            } else {
                print("api.getData failed")
                print(error as! NSError)
            }
        })
    

    在响应中,value在Story key下有多个字典。

    {
        "name" = "name";
        line = "some name";
        "image" = "https://";
       }
    {
        "name" = "name";
        line = "some name";
        "image" = "https://";
       }
    

    以及API回调方法

    class DataViewViewModel {
    
        typealias DataCompletion = (String) -> Void
    
        func getData(completionHandler: ((Array<Any>?, Error?) -> Void)!) -> Void {
    
            let url = URL(string: mainDataURL)
            URLSession.shared.dataTask(with:url!) { (data, response, error) in
                if error != nil {
                    print(error?.localizedDescription ?? "Error")
                    return completionHandler(nil, error)
                } else {
                    do {
                        let parsedData = try JSONSerialization.jsonObject(with: data!) as! [String:Any]
                        let itemsArray = parsedData["items"] as! Array<Any>
                        print(itemsArray)
                       return completionHandler(itemsArray, nil)
                    } catch let error as NSError {
                        print(error)
                    }
                }
                }.resume()
    
        }
    
    }
    

    有什么建议吗?

    enter image description here

    0 回复  |  直到 6 年前