博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS中NSBundle的使用
阅读量:6941 次
发布时间:2019-06-27

本文共 1265 字,大约阅读时间需要 4 分钟。

1、[NSBundle mainBundle],文件夹其实是Group,如左侧的树形文件管理器

Build之后,文件直接就复制到了根目录下,于是读取的方法,应该是这样:

NSString 
*earth = [[
NSBundle 
mainBundle] pathForResource:@
"Brad Cox" 
ofType:@
"png"
];

2、使用文件夹的时候,Build结果

依然使用同样的方法,不需要制定文件夹路径

//inDirectory参数可有可无
NSString *earth = [[NSBundle mainBundle] pathForResource:@"Matthijs" ofType:@"png" inDirectory:@"Images/MoreImages"]; NSString *earth = [[NSBundle mainBundle] pathForResource:@"Matthijs" ofType:@"png"];

 

编译之后,mainBundle的资源都是放到RootFolder下,所以,可以直接访问,不要指定内部路径

 

3、使用其他的Bundle

 

NSString 
*resourceBundle = [[
NSBundle 
mainBundle] pathForResource:@
"Resources" 
ofType:@
"bundle"
];
    
NSLog
(@
"resourceBundle: %@"
, resourceBundle);
    
NSString 
*earth = [[
NSBundlebundleWithPath
:resourceBundle] pathForResource:@
"Matthijs"
ofType:@
"jpg"
inDirectory:@
"Images/MoreImages"
];
 
    
NSLog
(@
"path: %@"
, earth);

使用Custom bundle,访问内部的子文件夹,需要指定inDirectory参数,这个mainBundle不同,这是为何?

注意:Since
bundles other than the main bundle can have folders embedded inside them, to access
files inside folders of a bundle other than the main bundle it is best to use the pathFor
Resource:ofType:inDirectory: method of
NSBundle 
to explicitly specify the folder in
which a specific file/resource exists.

 

原文链接:http://www.cnblogs.com/iihe602/archive/2013/01/17/2865280.html

你可能感兴趣的文章
vim/vi的文件内、跨文件复制粘贴操作、替换操作
查看>>
java命令执行jar文件
查看>>
python get class base name
查看>>
论这场云盘大战,以及各网盘的优劣
查看>>
怪异的JavaScript系列(三)
查看>>
辅助模式最终考验的是想象力,先来看看怎么用!| Accessibility
查看>>
TiDB 源码阅读系列文章(九) Hash Join
查看>>
【个人向整理】Promise
查看>>
用 Algolia DocSearch 轻松实现文档全站搜索
查看>>
Kibana 用户指南(安装Kibana)
查看>>
阿里CEO张勇:我们理解AI是Alibaba Intelligence
查看>>
mysql分表、分区
查看>>
关于do_brk()分析
查看>>
使用原生JS进行前后端同构
查看>>
【MySQL学习笔记】 MySQL 建立索引
查看>>
Mac开发React Native ---环境搭建
查看>>
[译] 如何手动启动 Angular 程序
查看>>
【数据结构】Java语言描述-单链表的基本操作
查看>>
[翻译]基于Webpack4使用懒加载分离打包React代码
查看>>
Android系统源码分析-JNI
查看>>