获取path的几种方式:NSFileManager NSHomeDirectory NSBundle
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *homeDir= NSHomeDirectory();//获取AppHome目录
NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];//获取document目录然后将文件名追加进去
NSString *tmpPath = NSTemporaryDirectory(); //获取tmp目录
NSLog(@"%@",tmpPath);
//在Documents下面创建一个test目录--------------------------------------------------------------
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@"test"];
BOOL ok = [fileManager createDirectoryAtPath:myDirectory attributes:nil];
NSLog(@"%@",myDirectory);
//取得一个目录下的所有文件名-------------------------------------------------------------------
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDir = [documentPaths objectAtIndex:0];
NSError *error = nil;
NSArray *fileList = [[NSArray alloc] init];
//fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
fileList = [fileManager contentsOfDirectoryAtPath:documentDir error:&error];
NSLog(@"%@",fileList);
//创建文件--------------------------------------------------------------------------------------
//NSString *myDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/test"];
//更改到待操作的目录下
[fileManager changeCurrentDirectoryPath:[tmpPath stringByExpandingTildeInPath]];//切换当前目录为document目录
//创建文件fileName文件名称,contents文件的内容,如果开始没有内容可以设置为nil,attributes文件的属性,初始为nil
[fileManager createFileAtPath:@"fileName"contents:nil attributes:nil];
//---------------------------------------------------------------------------------
[pool drain];
return 0;
}
bundle是一个目录,其中包含了程序会使用到的资源. 这些资源包含了如图像,声音,编译好的代码,nib文件(用户也会把bundle称为plug-in). 对应bundle,cocoa提供了类NSBundle.
我们的程序是一个bundle. 在Finder中,一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle
bundle中的有些资源可以本地化.例如,对于foo.nib,我们可以有两个版本: 一个针对英语用户,一个针对法语用户. 在bundle中就会有两个子目录:English.lproj和French.lproj,我们把各自版本的foo.nib文件放到其中. 当程序需要加载foo.nib文件时,bundle会自动根据所设置的语言来加载. 我们会在16章再详细讨论本地化
通过使用下面的方法得到程序的main bundle
NSBundle *myBundle = [NSBundle mainBundle];
一般我们通过这种方法来得到bundle.如果你需要其他目录的资源,可以指定路径来取得bundle
NSBundle *goodBundle;
goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];
一旦我们有了NSBundle 对象,那么就可以访问其中的资源了
// Extension is optional
NSString *path = [goodBundle pathForImageResource:@"Mom"];
NSImage *momPhoto = [[NSImage alloc] initWithContentsOfFile:path];
bundle中可以包含一个库. 如果我们从库得到一个class, bundle会连接库,并查找该类:
Class newClass = [goodBundle classNamed:@"Rover"];
id newInstance = [[newClass alloc] init];
如果不知到class名,也可以通过查找主要类来取得
Class aClass = [goodBundle principalClass];
id anInstance = [[aClass alloc] init];
可以看到, NSBundle有很多的用途.在这当中, NSBundle负责(在后台)加载nib文件. 我们也可以不通过NSWindowController来加载nib文件, 直接使用NSBundle:
BOOL successful = [NSBundle loadNibNamed:@"About" owner:someObject];
注意噢, 我们指定了一个对象someObject作为nib的File's Owner
使用initWithContentsOfFile时,文件路径的写法使用initWithContentsOfFile方法可以通过读取一个文件的内容来初始化对象。 但文件的路径应该怎么确定呢?可以使用NSBundle的对象来获取。例如当前程序所在目录下有个文件re.xml,我们要将该文件的内容做为NSData的数据源来初始化一个NSData对象,可以用下面的方法来实现: |
NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"]; NSData *data = [[NSData alloc] initWithContentsOfFile:filePath]; |
读取plist中的内容: |
NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; self.data = [NSArray arrayWithContentsOfFile:dataPath]; |
删除本地文件 |
NSString * thePath=[self getUserDocumentDirectoryPath]; |
NSMutableString * fullPath=[[[NSMutableString alloc]init]autorelease]; |
[fullPath appendString:thePath]; |
NSString * idString=[idArray objectAtIndex:indexPath.row]; |
NSString * coverName=[NSString stringWithFormat:@"/%@.jpg",idString]; |
[fullPath appendString:coverName]; |
NSFileManager *defaultManager; |
defaultManager = [NSFileManager defaultManager]; |
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error BOOL boolValue=[defaultManager removeItemAtPath: fullPath error: nil]; |
if (boolValue) { |
NSLog(@"remove cover image ok"); |
} |
- (NSString*)getUserDocumentDirectoryPath { |
NSArray* array = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask,YES); |
if([array count]) |
return [array objectAtIndex: 0]; |
else return @""; |
|
获取path的几种方式:NSFileManager NSHomeDirectory NSBundle的更多相关文章
- java:struts框架2(方法的动态和静态调用,获取Servlet API三种方式(推荐IOC(控制反转)),拦截器,静态代理和动态代理(Spring AOP))
1.方法的静态和动态调用: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...
- 获取Type的三种方式
using System;using UnityEngine; public class Type_Test : MonoBehaviour{ private void Awake() { ...
- java动态获取WebService的两种方式(复杂参数类型)
java动态获取WebService的两种方式(复杂参数类型) 第一种: @Override public OrderSearchListRes searchOrderList(Order_Fligh ...
- AngularJS中获取数据源的几种方式
在AngularJS中,可以从$rootScope中获取数据源,也可以把获取数据的逻辑封装在service中,然后注入到app.run函数中,或者注入到controller中.本篇就来整理获取数据的几 ...
- java 获取时间戳的三种方式
java 获取时间戳的三种方式 CreationTime--2018年7月13日16点29分 Author:Marydon 1.实现方式 方式一:推荐使用 System.currentTimeMi ...
- 【Struts2】Struts2获取session的三种方式
1.Map<String,Object> map = ActionContext.getContext().getSession(); 2.HttpSession session = S ...
- js获取时间戳的三种方式
js获取时间戳的三种方式 CreateTime--2018年5月23日08:44:10 Author:Marydon // 方式一:推荐使用 var timestamp=new Date().ge ...
- Struts2(四.注册时检查用户名是否存在及Action获取数据的三种方式)
一.功能 1.用户注册页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- HTTP获取信息的四种方式
HTTP 从网络获取信息的四种方式 GET GET指代你在浏览器中输入网址,浏览网站时做的事.例如,我们使用 http://www.baidu.com 的时候,可以将GET想象成他说:"hi ...
随机推荐
- Linux学习-开机过程的问题解决
忘记 root 密码的解决之道 新版的 systemd 的管理机制中,默认的 rescue 模式是无法直接取得 root 权限的喔!还是得要 使用 root 的密码才能够登入 rescure 环境.没 ...
- ssh登陆之忽略known_hosts文件
在平时工作中,有时候需要SSH登陆到别的Linux主机上去,但有时候SSH登陆会被禁止,并弹出如下类似提示: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- mantisbt邮件配置
PHP.INI里面 [mail function]; For Win32 only.#SMTP = 192.168.0.249SMTP = smtp.163.comsmtp_port = 25 ; F ...
- 学习boundingRectWithSize:options:attributes:context:计算文本尺寸
oundingRectWithSize:options:context: 返回文本绘制所占据的矩形空间. - (CGRect)boundingRectWithSize:(CGSize)size opt ...
- day03_08 变量的重新赋值02
python自动回收垃圾内存,不用了自动会回收,但是C不会 以下del代码为手动强拆,就是从内存中删除变量名
- TOJ4277: Sequence 组合数学
4277: Sequence Time Limit(Common/Java):2000MS/6000MS Memory Limit:65536KByte Total Submit: 39 ...
- JS数组的下标如果是字符串的排序
var test = []; test['0'] = 0; test['1'] = 1; test['2'] = 2; 这样一个数组的排序方式是字符为‘1’的数组元素排第一,为‘0’的排在最后
- deque 类
题外: 'A' +1='B' 1.deque被称为双端队列,它也是一种顺序容器.可通过迭代器存取元素 ,也可以通过下标顺序 存取元素 for(i=0;i<d1.size();i++) { cou ...
- 近期JS心得
child和tags都是[{id:1,value:'a'}]的格式,当点击一级标签,要看二级标签是否已经被选中,如果被选中,则清除出去 如果用for循环 再splice的话 当删除掉了一个元素后,数组 ...
- BZOJ1150 [CTSC2007]数据备份Backup 【堆 + 链表】
题目 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味 的,因此你想设计一个系统让不同的办公楼彼此之间互相备份,而你则坐在家中尽享计算机游戏的 ...