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);
    }

  • 相关阅读:
    事务及存储过程
    索引细讲
    数据库练习题
    position: absolute 或 display:table 的水平垂直居中
    bootstrap table 文字超出父div范围
    css 图片不定大小不压缩、不变形的对齐
    vue3.0 + svg 图标
    vue eslint(indent) 空格缩进报错
    vue3.0 + fontAwesome 图标
    vue3.0 + ts + element-plus + i18n 中英文切换
  • 原文地址:https://www.cnblogs.com/vonk/p/4275438.html
Copyright © 2011-2022 走看看