线程1:EXC_BREAKPOINT(代码= 1,子代码= 0x10136bb50) - swift

时间:2017-10-13 09:43:27

标签: swift xcode swift3

当我想使用GET方法发送值时出现此错误:

  

主题1:EXC_BREAKPOINT(代码= 1,子代码= 0x10136bb50)

获取值:

var flname      = self.txt_field_flname.text
var job_title   = self.txt_field_job_title.text
var mobile      = self.txt_field_mobile.text
var des         = self.txt_field_des.text
var lat         = self.lat
var lon         = self.lon

self.sendNewJob(fname: flname!, title: job_title!, mobile:  mobile!, des: des!, lat: String(lat), lon: String(lon) )
func sendNewJob(fname:String,title:String,mobile:String,des:String,
                lat:String,lon:String)
{
    print("fname \(fname)  title \(title)  mobile \(mobile) des \(des) lat \(lat) lon \(lon)") //output is well

    RestApiManager.sharedInstance.sendNewJob(fname: fname,title: title,mobile:mobile,
                                             des:des,lat:lat,lon:lon) { (json: JSON) in

    }
}
func sendNewJob(fname:String,title:String,mobile:String,des:String,
                lat:String,lon:String,onCompletion: @escaping (JSON) -> Void) {
    let route = baseURL+"up=1&Name=\(fname)&BusinessName=\(title)&MobileNumber=\(mobile)&latitude=\(lat)&longitude=\(lon)&Description=\(des)"
    makeHTTPGetRequest(path: route, onCompletion: { json, err in
        onCompletion(json as JSON)
    })
}


// MARK: Perform a GET Request
private func makeHTTPGetRequest(path: String, onCompletion: @escaping ServiceResponse) {
    let request = NSMutableURLRequest(url: NSURL(string: path)! as URL) // line of my error

    let session = URLSession.shared

    let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in
        if let jsonData = data {
            let json:JSON = JSON(data: jsonData)
            onCompletion(json, error as NSError?)
        } else {
            onCompletion(nil, error as NSError?)
        }
    })
    task.resume()
}

enter image description here

2 个答案:

答案 0 :(得分:0)

当代码执行nil值时会发生这种情况。这里代码为NSURL(string:path)!价值可能是零。您可以使用可选绑定(如果允许)来检查NSURL是否有效。当字符串无效且无法生成有效的URL时,就会发生这种情况。

答案 1 :(得分:0)

您可以这样使用:

private func makeHTTPGetRequest(path: String, onCompletion: @escaping ServiceResponse) {
    if let encodedPath = path.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
        if let url = URL(string: encodedPath) {
            let request = URLRequest(url: url)
            let session = URLSession.shared
            let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error -> Void in
                if let jsonData = data {
                    let json:JSON = JSON(data: jsonData)
                    onCompletion(json, error as NSError?)
                } else {
                    onCompletion(nil, error as NSError?)
                }
            })
            task.resume()
        } else {
            print("url is nil")
            onCompletion(nil)
        }
    } else {
        print("unable to encode url")
        onCompletion(nil)
    }
}