URLWithString 移除部分 NSString 路径

URLWithString removes part of NSString path

我在使用 CHCSVParser 解析 csv 文件时遇到问题。我的 csv 文件没有 "satisfy the stream" 我认为问题是文件的 URL 是错误的。我得到这样的 URL:

NSString *path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"csv"]];
NSURL *url = [NSURL URLWithString:path];

当我登录 path 时,我得到这个字符串:

/Users/wilhelmmichaelsen/Library/Developer/CoreSimulator/Devices/11F0BE77-9179-4A7B-B03E-1143957A8D02/data/Containers/Bundle/Application/7FE0AD10-DE7A-4C6D-9EED-4A95F9C197AE/Climate.app/file.csv

当我登录 url 时,我得到这个字符串:

/Users/wilhelmmichaelsen/Library/Developer/CoreSimulator/Devices/11F0BE77-9179-4A7B-B03E-1143957A8D02/data/Containers/Bu ... le.csv

路径字符串似乎被缩短了 (...)。这是不是很奇怪,我该如何解决?

您应该使用 fileURLWithPath 从路径创建 URL。

您使用了 URLWithString 方法,此方法要求 URLString 仅包含格式正确的 URL 中允许的字符。所有其他字符必须正确转义百分比。任何百分比转义字符都使用 UTF-8 编码进行解释。

要为文件系统路径创建 NSURL 个对象,请改用 fileURLWithPath:isDirectory:

Here is the referece of the Apple Developers Library Documentation for NSURL Class