获取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 ...
随机推荐
- HUD:4405-Aeroplane chess(期望飞行棋)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...
- django 缓存 实现
由于Django构建得是动态网站,每次客户端请求都要严重依赖数据库,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,缓存将一个某个views的返回值保存至内存或者memcache中, ...
- loj2012 「SCOI2016」背单词
-- #include <algorithm> #include <iostream> #include <cstring> #include <cstdio ...
- luogu2455 [SDOI2006]线性方程组 高斯消元法
#include <iostream> #include <cstdio> #include <cmath> using namespace std; int n, ...
- [POJ 1008] Maya Calendar C++解题
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 62297 Accepted: 192 ...
- python算法-汉诺塔问题
汉诺塔问题 初始状态: 思考:当盘子的个数是3的时候,大家写出移动顺序 移动的步骤: 3个盘子,从a到c 1.前面两个盘子,从a到b 1)把前面一个盘子,从a到c a->c 2)把第二个盘子 ...
- day01_06.比较运算符
> >= = < <= != == === !== 凡运算,必有运算结果,比较运算符的运算结果是布尔值 ==和===的区别 <?php $c = ( 3 == ...
- 洛谷9月月赛II 赛后瞎写
看错比赛时间了....结果发现的时候已经开始了半个小时,并且当时正准备睡午觉qwq 于是就水了个t1就 去睡 跑了 T2 写着写着然后看了一发评讲被辣鸡思路给绕了进去最后发现自己宛若一个智障 类似桶的 ...
- 【LeetCode】Binary Tree Postorder Traversal(二叉树的后序遍历)
这道题是LeetCode里的第145道题. 题目要求: 给定一个二叉树,返回它的 后序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很 ...
- TensorFlow——交互式使用会话:InteractiveSession类
目的是在交互式环境下(如jupyter),手动设定当前会话为默认会话,从而省去每次都要显示地说明sess的繁琐,如:Tensor.ecal(session=sess)或sess.Operation.r ...