NSLog与某些NSURL-iOS 9.2崩溃 [英] NSLog crashes with certain NSURL- iOS 9.2

查看:98
本文介绍了NSLog与某些NSURL-iOS 9.2崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,发生崩溃: -

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

应用程序崩溃 EXC_BAD_ACCESS

推荐答案

NSLog()的第一个参数格式字符串,包含
格式说明符(以开头),它们由
后面的变量参数列表扩展。在您的情况下,%20C 是格式说明符,但
不提供匹配参数。这会导致未定义的行为,
它可能会崩溃或产生不完整或错误的输出。

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.

如果你想使用 NSLog()然后一般的安全方法是

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

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

在这种特殊情况下,

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

同样适用,因为 NSURL NSObject 子类,可以使用
使用%@ 格式。

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

这篇关于NSLog与某些NSURL-iOS 9.2崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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