如何在moveItemAtPath:toPath:error:方法中从目录中区分文件

问题描述

|| 该方法的声明是: -(BOOL)moveItemAtPath:(Nsstring *)srcPath到Path:(Nsstring)dstPath错误:(NSError *)错误 及其文档说源路径可以是文件名或目录名,假设我们在当前工作目录中有一个名为\“ test \”的文件一个名为\“ test \”的子目录,方法: [fm moveItemAtPath:@ \“ test \” toPath:@ \“ newTest \”错误:空]; 其中fm是NSFileManager类的对象,哪个项目将被重命名文件还是目录?此方法如何区分目录中的文件? ps。据我所知,如果源路径和目标路径相同,则此方法重命名在这种情况下,源路径=目标路径=当前工作目录。     

解决方法

正如@stam所说,您无法创建具有相同名称的文件和文件夹(请注意,
file.txt
file
不同)。 但是,如果您想知道路径代表目录还是文件,请使用以下方法:
- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory
像:
BOOL isDir;
if ([fm fileExistsAtPath:path isDirectory:&isDir]) {
    // Item Exists
    if (isDir) {
        // Is a directory
    } else {
        // Is a file
    }
} else {
    // Item doesn\'t exist
}