崩溃时出现错误libc ++ abi.dylib:以NSException类型的未捕获异常终止

时间:2016-12-16 13:56:38

标签: swift crash closures

Screenshot您好我在块内部调用块但是app崩溃了,错误是libc ++ abi.dylib:以NSException类型的未捕获异常终止。控制台上没有打印其他说明。无法找到问题。

        GeocoderApi.shared.getAddress(lat: "\(coordinate.latitude)", lang: "\(coordinate.longitude)", reverseGeocodeHandler: { status, responseObject in
        if let locality = responseObject as? String{
            WeatherApi.shared.averageForecastName(cityName: locality, completionHandler: { status, responseObject in
                LocationManager.shared.stopUpdatingLocation()
                if let weatherArray = responseObject as? [Weather]{
                    if !weatherArray.isEmpty {
                        self.myJourney.weatherArray.remove(at: 0)
                        self.myJourney.weatherArray.insert(weatherArray[0], at: 0)
                        self.myJourney.weatherArray[0].upcomings.append(contentsOf: weatherArray)
                        self.myJourney.weatherArray[0].upcomings.remove(at: 0)
                        self.journeyView.reloadWeather()
                    }
                }
            })
        }
    })

func getAddress(lat:String,lang:String,reverseGeocodeHandler:@escaping (_ status:Bool, _ responseObject:Any)->()){

    if !ReachabilityManager.shared.isReachable {
        let error = WSError()
        error.errorTitle = "Network error"
        error.errorDescription = "Unable to connect, please check your internet connectivity."
        reverseGeocodeHandler(false,error)
        return
    }

    self.showNetworkActivity()

    let sessionManager = AFHTTPSessionManager(baseURL: baseUrl, sessionConfiguration: URLSessionConfiguration.default)
    sessionManager.requestSerializer = AFJSONRequestSerializer()
    sessionManager.responseSerializer = AFJSONResponseSerializer()
    let urlPath =  getPath(path: "maps/api/geocode/json?latlng=\(lat),\(lang)")

    sessionManager.post(urlPath, parameters: nil, progress: { progress in

    }, success: { task, responseObject in
        var cityName = ""

        if let disc = responseObject as? [String:Any]{
            cityName = self.Parse(disc: disc)
        }
        self.hideNetworkActivity()

        reverseGeocodeHandler(true,cityName)

    }, failure: { operation, err in
        self.hideNetworkActivity()
        let error = WSError(error: err as NSError)
        reverseGeocodeHandler(false,error)

    })
}

func averageForecastName(cityName:String,completionHandler:@escaping (_ status:Bool, _ responseObject:Any)->()){

    if !ReachabilityManager.shared.isReachable {
        let error = WSError()
        error.errorTitle = "Network error"
        error.errorDescription = "Unable to connect, please check your internet connectivity."
        completionHandler(false,error)
        return
    }

    self.showNetworkActivity()

    let sessionManager = AFHTTPSessionManager(baseURL: baseUrl, sessionConfiguration: URLSessionConfiguration.default)
    sessionManager.requestSerializer = AFJSONRequestSerializer()
    sessionManager.responseSerializer = AFJSONResponseSerializer()
    let urlPath =  getPath(path: "data/2.5/forecast/daily?q=\(cityName)&cnt=6")

    sessionManager.post(urlPath, parameters: nil, progress: { progress in

    }, success: { task, responseObject in
        var weatherArray = [Weather]()

        if let disc = responseObject as? [String:Any]{
            weatherArray.append(contentsOf: Weather.GetForecasts(rootDisc: disc))
        }
        self.hideNetworkActivity()

        completionHandler(true,weatherArray)

    }, failure: { operation, err in
        self.hideNetworkActivity()
        let error = WSError(error: err as NSError)
        completionHandler(false,error)

    })
}

1 个答案:

答案 0 :(得分:0)

通过.addingPercentEncoding解决它(withAllowedCharacters:.urlQueryAllowed)