【问题标题】:Why does my file URL (with relative path) fail even when using stringByExpandingTildeInPath:?为什么即使使用 stringByExpandingTildeInPath: 我的文件 URL(带有相对路径)也会失败?
【发布时间】:2013-09-05 19:42:38
【问题描述】:

我正在尝试创建一个有效的文件 URL,它指向一个我知道存在的文件。

NSURL* fileURL = [NSURL fileURLWithPath:[@"~/Users/zak/global.scss" 
stringByExpandingTildeInPath] isDirectory:NO];
NSLog(@"Looks like you want me to open a %@ file!", fileURL.pathExtension);

NSError *err;
if (![fileURL checkResourceIsReachableAndReturnError:&err]) {
    NSLog(@"%@", err);
}
else {
    NSLog(@"success!!!");
};

这总是导致编译器不断告诉我

Error Domain=NSCocoaErrorDomain Code=260 "The file “global.scss” couldn’t be 
opened because there is no such file." UserInfo=0x1021937a0 {NSURL=file://
localhost/~/Users/zak/global.scss, 
NSFilePath=/~/Users/zak/global.scss, 
NSUnderlyingError=0x10212f4b0 "The operation couldn’t be completed.
No such file or directory"}

为什么这不起作用?我是否需要使用某种常量作为基本 URL?

【问题讨论】:

    标签: objective-c macos file-io path


    【解决方案1】:

    ~/Users/zak/globals.scss 几乎可以肯定存在。 ~ 在一个看起来很奇怪的地方。你可以试试~zak/global.scss/Users/zak/globals.scss。要检查路径,只需在终端窗口中执行ls PATHTOTEST - 它会告诉您它是否存在(如果失败,则会向您显示路径的扩展)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-21
      • 2011-10-04
      • 1970-01-01
      • 2018-05-15
      • 2021-08-21
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多