【问题标题】:%2C URL causing iOS app crash%2C URL 导致 iOS 应用程序崩溃
【发布时间】:2013-09-17 20:29:52
【问题描述】:

我有一个 iOS 应用程序,它从该 URL 下载 JSON 提要:

https://www.googleapis.com/youtube/v3/activities?part=snippet%2CcontentDetails&home=true&maxResults=50&access_token=%@

我将 URL 存储在 NSString 中以供以后使用。我还在 URL 的末尾添加了一个 NSString,其中包含我用于 OAuth 身份验证的访问令牌(因此 %@ 在 URL 的最后)。

这是我存储 URL 的方式:

NSString *pre_yt_user_url = [NSString stringWithFormat:@"https://www.googleapis.com/youtube/v3/activities?part=snippet%2CcontentDetails&home=true&maxResults=50&access_token=%@", token_youtube];

如您所见,部分 URL 有一个 %2C

这会导致警告并使我的 iOS 应用程序崩溃!!

这是我收到的警告:

Format specifies type 'unsigned-short' but the argument has type NSString

和:

More % conversions than data arguments

我在这里做错了什么?我不能将 URL 存储在字符串中吗??

谢谢,丹。

【问题讨论】:

    标签: ios cocoa-touch url nsstring nsstringencoding


    【解决方案1】:

    当使用stringWithFormat 时,% 字符是数据参数的开头,除非它被转义。因此,您需要对其进行转义,因为您不想将其用作提供的参数。您需要使用%%2C(因为第一个% 转义了第二个%)。

    【讨论】:

    • 对了。我没有意识到这一点。非常感谢 :) 一旦 Stack Overflow 允许我这样做,我会确保我勾选这个答案 :)
    猜你喜欢
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    相关资源
    最近更新 更多