通过NSLog打印NSError总是会引发EXE_BAD_ACCESS [英] Printing NSError by NSLog always raises EXE_BAD_ACCESS

查看:439
本文介绍了通过NSLog打印NSError总是会引发EXE_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于检索远程json文件的代码.我尝试在网络不可用时打印错误(故意在飞行模式下使用以产生错误).

I have a code for retriving remote json file. I tried to print the error when the network is not available (turing on airplane mode on purpose to produce the error).

但是每次它都会在日志记录行上引发EXE_BAD_ACCESS

But every time, it raised a EXE_BAD_ACCESS on the logging line

我的代码是这样的:

        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
(response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in

            if(error != nil){
                let errorDesc = error!.description ?? ""

                NSLog("HTTP request error = \(error!.code), desc = \(errorDesc)")

                handler(houseList, error)
                return
            } 
            ...Omitted for Brevity...
    } 

NSLog("HTTP request error = \(error!.code), desc = \(errorDesc)")是引发错误的行

我的观察:

  • 首先,在我的调试窗口中,error.description看起来 很好.我可以毫无问题地看到数据.

  • First of all, in my debug window the error.description looks perfectly fine. i can see the data with no problems.

其次,如果我将NSLog更改为print(),则一切正常 很好!

Secondly, if i change NSLog to print(), then everything works just fine!

最后,如果我将原始行更改为以下内容,它也可以工作

Lastly, if I change the original line to the following, it works too

NSLog("HTTP请求错误=(错误!.code),desc =%@",errorDesc)

NSLog("HTTP request error = (error!.code), desc = %@", errorDesc)

是因为NSlog无法正确处理字符串插值吗?

Is it because NSlog cannot process String Interpolation correctly?

如果是这种情况,那么为什么在某些情况下(例如"HTTP request error = \(error!.code))它可以工作?

If this is the case, then why in some cases such as "HTTP request error = \(error!.code), it can work ?

我的开发环境: Xcode:版本7.0.1(7A1001) OS X:优胜美地10.10.5(14F27

My Dev Environment: Xcode: Version 7.0.1 (7A1001) OS X: Yosemite 10.10.5(14F27

谢谢您的时间!! :)

Thank you for your time!! :)

推荐答案

NSLog()的第一个参数是格式字符串,与 C库中的printf()函数.此格式字符串用于 作为模板,并包含 格式说明符 例如%d%s%@,....格式指定 其余的参数将被解释.

The first argument of NSLog() is a format string, similar as in the printf() function from the C library. This format string serves as a template and contains format specifiers such as %d, %s, %@, ... The format specifies how the remaining arguments are interpreted.

现在就您而言,

NSLog("HTTP request error = \(error!.code), desc = \(errorDesc)")

错误代码和描述将插入到格式字符串中. 如果错误描述恰好包含格式说明符(例如%s),则NSLog()需要另一个参数(例如字符串). 如果没有参数或类型错误,则行为为 未定义,代码可能会崩溃.

the error code and description are interpolated into the format string. If the error descriptions happens to contain a format specifier (e.g. %s) then NSLog() expects another argument (e.g. string). If there is no argument or if it has the wrong type, the behavior is undefined and the code may crash.

因此,格式字符串应始终为常量字符串文字:

Therefore, the format string should always be a constant string literal:

NSLog("HTTP request error = %ld, desc = %@", error!.code, errorDesc)

如果要利用Swift字符串插值 然后仅使用%@作为格式并传递插值的String 作为附加参数:

If you want to take advantage of the Swift string interpolation then use just %@ as the format and pass the interpolated String as additional argument:

NSLog("%@", "HTTP request error = \(error!.code), desc = \(errorDesc)")

这篇关于通过NSLog打印NSError总是会引发EXE_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆