zoukankan      html  css  js  c++  java
  • 【iOS】路径获取

    【问题描述】

    iOS应用中,主要有两种路径,一是Documents目录(即应用安装的路径),二是Bundle路径(即应用程序束)

    一、获取Documents路径

    - (NSString *)filePathInDoc:(NSString *)filename
    {
        NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
        NSString *documentDirectory=[paths objectAtIndex:0];
        return [documentDirectory stringByAppendingPathComponent:filename];
    }

    那么获取Doc目录下的文件Text.txt,如下:

    [self filePathInDoc:@"Text.txt"];

    二、获取Bundle路径

    - (NSString *)getFilePathInBundle:(NSString *)filename
    {
        NSString *nameWithoutExt = [filename stringByDeletingPathExtension];
        NSString *ext = [filename pathExtension];
        return [[NSBundle mainBundle] pathForResource:nameWithoutExt ofType:ext];
    }

    //- (NSString *)filePathInBundle:(NSString *)filename
    //{
    //    int index = -1;
    //    
    //    for (int loop = [filename length] - 1; loop >= 0; loop--)
    //    {
    //        if ([filename characterAtIndex:loop] == '.')
    //        {
    //            index = loop;
    //            break;
    //        }
    //    }
    //    
    //    if (index == -1)
    //    {
    //        return nil;
    //    }
    //    
    //    NSString *filenameWithoutExt = [filename substringToIndex:index];
    //    NSString *ext = [filename substringFromIndex:index + 1];
    //    NSString *path = [[NSBundle mainBundle] pathForResource:filenameWithoutExt ofType:ext];
    //    return path;
    //}

    那么获取Bundle下的Text.txt文件,如下:

    [self filePathInBundle:@"Text.txt"];

  • 相关阅读:
    shell关闭指定进程
    linux tricks 之数据对齐。
    linux tricks 之VA系列函数.
    linux tricks 之 typeof用法.
    linux下notify机制(仅用于内核模块之间的通信)
    怎么判定一个mac地址是multicast还是unicast.
    linux tricks 之 ALIGN解析.
    fid解释
    c语言中宏定义#和 ##的作用:
    rebtree学习
  • 原文地址:https://www.cnblogs.com/ftrako/p/2766884.html
Copyright © 2011-2022 走看看