English 中文(简体)
如何正确摆脱 %%% 符号
原标题:How to correctly escape a % symbol

无法正确格式化字符串 URL 。 想要的输出是 :

http://gdata.youtube.com/feeds/api/videos?q=corgi&start-index=1&max-results=50&v=2&fields=entry%5Blink/@rel= http://gdata.youtube.com/schemas/2007%23mobile %5D

这是我最初的代码:

NSString *urlRequest = [NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/videos?q=corgi&start-index=%u&max-results=%u&v=2&fields=entry[link/@rel= http://gdata.youtube.com/schemas/2007%23mobile ]", dataStartIndex, dataIncrements];
NSURL *url = [NSURL URLWithString:urlRequest];

它不停地在结尾处堵住% 23 移动器, 并使之成为 20072obile 。 我尝试了在 @ 符号之前使用一个符号, 但没有成功 。 我做错了什么?

奇怪的是,如果我把它破碎成两块,它的工作就是正确的:

NSString *urlRequest = [NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/videos?q=corgi&start-index=%u&max-results=%u&v=2&fields=entry", dataStartIndex, dataIncrements];

NSURL *url = [NSURL URLWithString:[urlRequest stringByAppendingString:@"[link/@rel= http://gdata.youtube.com/schemas/2007%23mobile ]"]];

如果我这样做没有任何论据(dataStartIndex, dataIncrements),

最佳回答

您需要用其他 来跳出 , 才能创建 2007__ 23move 。 例如 :

NSString *urlRequest = [NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/videos?q=corgi&start-index=%u&max-results=%u&v=2&fields=entry[link/@rel= http://gdata.youtube.com/schemas/2007%%23mobile ]", dataStartIndex, dataIncrements];
问题回答

您可以使用:

NSString *string=[@"yourUrlString" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

添加一个额外的% 以逃避%,因为您提供的字符串是一个格式字符串,而不是普通字符串。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签