Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos NSFileManager使用“重命名文件”;moveItemAtURL:“;名称包含路径分隔符,如;foo/bar.extension“;_Macos_Cocoa - Fatal编程技术网

Macos NSFileManager使用“重命名文件”;moveItemAtURL:“;名称包含路径分隔符,如;foo/bar.extension“;

Macos NSFileManager使用“重命名文件”;moveItemAtURL:“;名称包含路径分隔符,如;foo/bar.extension“;,macos,cocoa,Macos,Cocoa,这感觉很奇怪,我的代码非常简单 // something like "foo/bar" NSString *correctFileName = seriesDict[seriesNumber]; if (correctFileName.length > 0) { // So I'll have a fileName like "foo/bar.extension" which looks like a directory and a file in it... NSStri

这感觉很奇怪,我的代码非常简单

// something like "foo/bar"
NSString *correctFileName = seriesDict[seriesNumber];
if (correctFileName.length > 0)
{
    // So I'll have a fileName like "foo/bar.extension" which looks like a directory and a file in it...
    NSString *pathExtension = [filePath pathExtension];
    NSString *correctFilePath = [[[filePath stringByDeletingLastPathComponent]
                                    stringByAppendingPathComponent:correctFileName]
                                    stringByAppendingPathExtension:pathExtension];

    NSError *error = nil;
    [[NSFileManager defaultManager] moveItemAtPath:filePath toPath:correctFilePath error:&error];

    // And NSFileManager can not treat it as a legal fileName, kind of expected...   
    if (error)
    {
        NSLog(@"Rename file at %@ failed, error: %@", filePath, error);
    }
}
似乎可以在Finder中将我的文件重命名为“foo/bar.extension”,如下所示

也许有一种解决方案可以在代码中实现这一点


如果有人能在光线下切碎,我们将不胜感激。

Finder中的
/
将转换为
<代码>/在POSIX样式路径中无效,而
在HFS样式路径中无效,因此macOS会将这两个字符相互映射

技术上正确的方法是使用
cfurlcreateWithfilesystemtobase
创建
CFURL
,将
kCFURLHFSPathStyle
指定为路径样式,并根据已创建的基本URL进行解析。然后使用
cfurlcopyfilesystems
复制完整URL的路径


不过,从实用角度讲,您可以简单地在
/
之间进行字符串替换:

Finder允许您这样做,但它实际上并没有在文件名中添加目录分隔符,它只是向您显示一个。如果您
ls
包含该文件的目录,您将看到真实名称是“foo:bar.mkv”。目录分隔符在文件名中无效。非常感谢,现在我明白了。