我有一个从这个 URL 下载 JSON 提要的 iOS 应用程序:
https://www.googleapis.com/youtube/v3/activities?part=snippet%2CcontentDetails&home=true&maxResults=50&access_token=%@
我将 URL 存储在 NSString 中供以后使用。我还在 URL 的末尾添加了一个 NSString,其中包含我用于 OAuth 身份验证的访问 token (因此 %@ 在 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 存储在字符串中吗??
谢谢,丹。
最佳答案
当使用 stringWithFormat
时,%
字符是数据参数的开始,除非它被转义。所以你需要转义它,因为你不想将它用作提供的参数。您需要使用 %%2C
(因为第一个 %
转义了第二个 %
)。
关于ios - %2C 导致 iOS 应用程序崩溃的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18859336/