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

Posted

技术标签:

【中文标题】线程 1: EXC_BREAKPOINT (code=1, subcode=0x10136bb50) - swift【英文标题】:Thread 1: EXC_BREAKPOINT (code=1, subcode=0x10136bb50) - swift 【发布时间】:2017-10-13 09:43:26 【问题描述】:

当我想使用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()

【问题讨论】:

任何不小心写出的感叹号都是邀请:请让我崩溃。顺便说一句,这是 Swift 3:不要使用 NSMutableURLRequestNSURLNSError 并且变量名应该是 camelCased 而不是 snake_cased 0k。我认为NSURL 不支持 utf-8。使用 utf-8 字符串时出现此错误。 使用导致此崩溃的 path 的确切值更新您的问题。它很可能包含空格或其他无效的未转义字符。 【参考方案1】:

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

【讨论】:

我认为NSURL 不支持utf-8。使用 utf-8 字符串时出现此错误。【参考方案2】:

你可以这样使用:

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)
    

【讨论】:

以上是关于线程 1: EXC_BREAKPOINT (code=1, subcode=0x10136bb50) - swift的主要内容,如果未能解决你的问题,请参考以下文章

在 SpriteKit 中出现错误。 - 线程 1:EXC_BREAKPOINT(代码=1,子代码=0x1007351fc)

Xcode: 如何修复线程 5: EXC_BREAKPOINT (code=1, subcode=0x10025c76c)

线程 1: EXC_BREAKPOINT(code = EXC_I386_BPT, subcode=0x0

SwiftUI - WKWebView 的 iOS 13 UIViewRepresentable 获取线程 1:EXC_BREAKPOINT 崩溃

SwiftUI - WKWebView 的 iOS 13 UIViewRepresentable 获取线程 1:EXC_BREAKPOINT 崩溃

prepareForSegue 错误线程 1: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0)