本地 URL 中的转义空格



我有以下代码来尝试在可可应用程序中加载本地HTML页面...

NSString *basePath = @"file//Users/david/Documents/My Project/index.html";
NSString *escapedPath = [basePath stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL*url=[NSURL URLWithString:escapedPath];
NSURLRequest*request=[NSURLRequest requestWithURL:url];
NSLog(@"current file is %@", url);

不幸的是,url 总是空的,如果我查看 escapedPath 的值,我会看到"%20"转义空间。 但是,这在本地不起作用。 是否有一个 stringBy 函数可以正确转义本地路径?

谢谢!

对于本地路径,您需要使用 fileURLWithPath:

请参阅此处的文档:

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html

最新更新