zoukankan      html  css  js  c++  java
  • [iOS]リソースファイルの取得方法

    リソースファイルのパスを取得するためには下記のように実装する。

    --------------------------------------------------------------------------------
    ◯リソースファイルの取得方法
    --------------------------------------------------------------------------------
    ファイル名「Test.db」のパスを取得する。
    NSString *fileName = @"Test.db";
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString *filePath = [resourcePath stringByAppendingPathComponent:fileName];

    ただしローカライズした際に上記の方法ではデフォルトのファイルしか取得できない。
    そのため下記の方法で取得した方が良さげ。

    NSBundle *mainBundle = [NSBundle mainBundle]; // pathForResourceには拡張子を含めない。 NSString *filePath = [mainBundle pathForResource:@"Test" ofType:@"db"];

    ちなみにサブフォルダを作成した場合は下記のようにinDirectoryにサブフォルダ名を指定する。

    ?12 NSBundle *mainBundle = [NSBundle mainBundle]; NSString *filePath = [mainBundle pathForResource:@"Test" ofType:@"db" inDirectory:@"DB"];

    【リソースフォルダにサブフォルダを作る方法】
    リソースファイルをプロジェクトにコピーするときに「Create folder references for any added folders」を選択する。
    プロジェクトエクスプローラー上でフォルダが青くなっていればサブフォルダの作成に成功している。


    --------------------------------------------------------------------------------
    ◯プロジェクトの全リソースファイルを取得する方法
    --------------------------------------------------------------------------------
     
    NSError *error;
    NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
    NSArray *directory = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:resourcePath error:&error];
    for (NSString *str in directory) {     NSLog(@"FileName:%@",str);
    }

  • 相关阅读:
    吃推荐3个最近去了的好地方
    30岁生日
    今天开始试用Briglow Hair Cream
    WPF中如何在文本外面加虚线外框
    对账算法改进
    如何退出正在Sleep的线程
    关于framework4.5的相关介绍
    恐怖的报警邮件
    对帐引擎2个月后的监控数据
    wcf rest服务启用gzip压缩
  • 原文地址:https://www.cnblogs.com/vonk/p/4275438.html
Copyright © 2011-2022 走看看