S.M_Emamian
S.M_Emamian

Reputation: 17373

Thread 1: EXC_BREAKPOINT (code=1, subcode=0x10136bb50) - swift

I got this error when I want to send value with GET method:

Thread 1: EXC_BREAKPOINT (code=1, subcode=0x10136bb50)

To get values:

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

Upvotes: 2

Views: 15201

Answers (2)

Vini App
Vini App

Reputation: 7485

You can use like this :

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)
    }
}

Upvotes: 0

rajtharan-g
rajtharan-g

Reputation: 452

This happens when the code executes a nil value. Here the code NSURL(string: path)! value might be nil. You can use optional binding (if let) to check whether the NSURL is a valid one. It happens when the string is not valid and does not make a valid URL.

Upvotes: 1

Related Questions