xcode NSData & NSURL - 空间有问题的网址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1441106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSData & NSURL - url with space having problem
提问by Sagar R. Kothari
I have following code in my application.
我的应用程序中有以下代码。
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:pathOfThumbNail]];
pathOfThumbNail has following path
pathOfThumbNail 有以下路径
http://70.84.58.40/projects/igolf/TipThumb/GOLF58B.jpg
http://70.84.58.40/projects/igolf/TipThumb/GOLF58B.jpg
When I open above path in safari browser - path is changed automatically & image is successfully displayed.
当我在 safari 浏览器中打开上面的路径时 - 路径会自动更改并成功显示图像。
http://70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg
http://70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg
But in iPhone, due to space in path, image isn't loaded in nsdata.
但在 iPhone 中,由于路径中的空间,图像未加载到 nsdata 中。
回答by zaph
Use: stringByAddingPercentEscapesUsingEncoding:
使用:stringByAddingPercentEscapesUsingEncoding:
Returns a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.
使用给定的编码返回接收器的表示,以确定将接收器转换为合法 URL 字符串所需的转义百分比。
-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
A representation of the receiver using encoding to determine the percent escapes necessary to convert the receiver into a legal URL string. Returns nil if encoding cannot encode a particular character
使用编码来确定将接收器转换为合法 URL 字符串所需的转义百分比的接收器表示。如果编码无法编码特定字符,则返回 nil
Added per request by @rule
由@rule 为每个请求添加
NSString* urlText = @"70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg";
NSString* urlTextEscaped = [urlText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: urlTextEscaped];
NSLog(@"urlText: '%@'", urlText);
NSLog(@"urlTextEscaped: '%@'", urlTextEscaped);
NSLog(@"url: '%@'", url);
NSLog output:
NSLog 输出:
urlText: '70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg' urlTextEscaped: '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg' url: '70.84.58.40/projects/igolf/TipThumb/GOLF%2058B.jpg'
回答by Niko Zarzani
A swift 3.0 approach (stringByAddingPercentEscapesUsingEncoding and stringByAddingPercentEncodingWithAllowedCharacters seems now deprecated):
快速的 3.0 方法(stringByAddingPercentEscapesUsingEncoding 和 stringByAddingPercentEncodingWithAllowedCharacters 现在似乎已弃用):
let urlString ="your/url/".addingPercentEncoding( withAllowedCharacters: .urlQueryAllowed)
回答by Priest
stringByAddingPercentEscapesUsingEncoding
has been deprecated in iOS 9.0, it is recommended you use stringByAddingPercentEncodingWithAllowedCharacters
instead.
stringByAddingPercentEscapesUsingEncoding
已在 iOS 9.0 中弃用,建议您stringByAddingPercentEncodingWithAllowedCharacters
改用。
Here's the Objective-C code for > iOS 9.0
这是 iOS 9.0 的 Objective-C 代码
NSString* urlText = @"70.84.58.40/projects/igolf/TipThumb/GOLF 58B.jpg";
NSString* urlTextEscaped = [urlText stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString: urlTextEscaped];
NSLog(@"urlText: '%@'", urlText);
NSLog(@"urlTextEscaped: '%@'", urlTextEscaped);
NSLog(@"url: '%@'", url);