【问题标题】:NSFileManager how to return folder name of file pathNSFileManager如何返回文件路径的文件夹名称
【发布时间】:2014-05-05 10:51:28
【问题描述】:

我有这条路:

/Users/mac/Library/Application Support/iPhone Simulator/7.0.3/Applications/5C6B84DE-2F18-48A8-AC4D-5C4973F19050/tmp/Root/Folder 1/test.pdf

我想从这个路径中提取/Users/mac/Library/Application Support/iPhone Simulator/7.0.3/Applications/5C6B84DE-2F18-48A8-AC4D-5C4973F19050/tmp/Root/Folder 1/

我可以使用NSFileManager 来获取包含 test.pdf 文件的目录路径还是我需要自己解析它。

【问题讨论】:

    标签: ios nsstring nsfilemanager


    【解决方案1】:

    您可以使用stringByDeletingLastPathComponentNSString 类来做到这一点:

    NSString *path = @"/Users/mac/Library/Application Support/iPhone Simulator/7.0.3/Applications/5C6B84DE-2F18-48A8-AC4D-5C4973F19050/tmp/Root/Folder 1/test.pdf";
    
    NSString *folder = [path stringByDeletingLastPathComponent];
    

    Reference

    stringByDeletingLastPathComponent

    返回通过从 接收器,以及任何最终路径分隔符。

    - (NSString *)stringByDeletingLastPathComponent

    返回值

    通过删除最后一个路径组件创建的新字符串 接收器,以及任何最终路径分隔符。如果接收方 表示原样返回的根路径。讨论

    请注意,此方法仅适用于文件路径(例如, URL 的字符串表示形式)。

    【讨论】:

      【解决方案2】:

      您可以使用stringByDeletingLastPathComponent

       NSString *path=@"";
         NSString *path1= [path stringByDeletingLastPathComponent];
      

      【讨论】:

        【解决方案3】:

        如果你想要没有文件名的文件路径,让我们像下面这样使用

        NSString * filePath = [path stringByDeletingLastPathComponent];
        

        【讨论】:

          【解决方案4】:

          使用 NSString 的类方法stringByDeletingLastPathComponent 删除最后一个路径组件。希望对你有帮助。

           NSString *path = @"/Users/mac/Library/Application Support/iPhone Simulator/7.0.3/Applications/5C6B84DE-2F18-48A8-AC4D-5C4973F19050/tmp/Root/Folder 1/test.pdf";
          
              path = [path stringByDeletingLastPathComponent];
          
              NSLog(@"path %@",path);
          

          【讨论】:

            猜你喜欢
            • 2017-12-22
            • 1970-01-01
            • 2020-12-19
            • 2016-12-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-04-13
            • 1970-01-01
            相关资源
            最近更新 更多