NSLog crashes with certain NSURL- iOS 9.2

。_饼干妹妹 提交于 2019-12-01 06:34:26

问题


Here is my code,where the crash occurs:-

let URL = NSURL(string: "http://files.parsetfss.com/fa80bc63-88d4-412d-a478-2451cffc92a9/tfss-1d2a321d-b02e-4745-a589-e31536f648df-XXXXX%20CAT15%2030.p0001.jpg")
NSLog("Loading page with URL: \(URL)")

The app crashes with EXC_BAD_ACCESS


回答1:


The first argument of NSLog() is a format string, and contains format specifiers (starting with %) which are expanded by the following variable argument list. In your case %20C is a format specifier, but no matching argument is supplied. That causes undefined behavior, it can crash or produce incomplete or wrong output.

If you want to use NSLog() then a general safe method is

NSLog("%@", "Loading page with URL: \(URL)")

In this particular case,

NSLog("Loading page with URL: %@", URL)

works as well, since NSURL is a NSObject subclass and can be used with the %@ format.




回答2:


You should use println instead of NSLog.

let URL = NSURL(string: "http://files.parsetfss.com/fa80bc63-88d4-412d-a478-2451cffc92a9/tfss-1d2a321d-b02e-4745-a589-e31536f648df-XXXXX%20CAT15%2030.p0001.jpg")!
println("Loading page with URL: \(URL)")

I have added the option sign ! at the end to unwrap.



来源:https://stackoverflow.com/questions/35335885/nslog-crashes-with-certain-nsurl-ios-9-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!