数据持久,数据共享,NSFileManager,NSFileHandler,NSBundle
2015-05-02 22:25
246 查看
1. ios模拟器目录保存位置
/Users/<youruser>/Library/Application Support/iPhone Simulator/4.3.2/Applications/<yourappguid>/Documents
对于一个运行在iPhone得app,它只能访问自己根目录下得一些文件(即sandbox).
详见apple文档:http://www.apple.com.cn/developer/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html
参考 http://www.cnblogs.com/drc606/archive/2011/12/18/iosfile.html
常量:
NSDocumentDirectory 存放目录:<Application_Home>/Documents
NSCachesDirectory <Application_Home>/Library/Caches
NSApplicationSupportDirectory <Application_Home>/Library/Application Support
NSUserDefaults <Application_Home>/Library/Preferences/<your App's bundle ID>.plist
1.应用程序根目录:NSDocumentDirectory
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsPath = [paths objectAtIndex:0];
NSString* documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString* documentsFilePath = [documentsPath stringByAppendingPathComponent:@"filename1.txt"];
2.应用程序目录下的缓存目录:NSCachesDirectory,Location of discardable cache files (/Library/Caches).
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* diskCachePath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"] retain];
3.临时目录NSTemporaryDirectory,它与[NSHomeDictionary() stringByAppendingPathComponent:@“tmp”]不同,tmp目录在里面写入一些程序运行时需要用得数据,里面写入得数据在程序退出后会没有.
+ (NSString*)tempPathWithFileName:(NSString*)filename
{
NSString* tempPath = NSTemporaryDirectory();
return [tempPath stringByAppendingPathComponent:filename];
}
The tmp dir is cleared each 3 days
The Caches directory is where you store cache files and other temporary data that your application can re-create as needed. This directory is located inside the Library directory.
Never store files at the top level of this directory: Always put them in a subdirectory named for your application or company.Your application is resp*****ible for cleaning out cache data files when they are no longer needed.
The system does not delete files from this directory.
To get the path to this directory use the NSCachesDirectory search path key with the NSUserDomainMask domain.
4.创建目录
if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath
withIntermediateDirectories:YES
attributes:nil
error:NULL];
}
5.资源
NSString* path=[[NSBundle mainBundle ] pathForResource:@"default" ofType:@"html"];
6. IOS文件系统及其相关操作(NSFileManager,NSFileHandle)
http://hi.baidu.com/lrorolove/blog/item/578afff57eac83c4f3d3857b.html
7.iOS中使用c函数快速获取一个目录所占的空间大小
/article/3932976.html
1.三种高级数据持久方式:属性列表,对象归档,嵌入式关系数据库SQLite3.
2.应用程序的沙盒:
每个应用程序都有自己的Documents文件夹,并且仅能读取各自的Documents目录中的内容.
/Library/Application Surpport/iPhone Simulator/3.1.2/Applications/目录下有Xcode生成的一些随机名字的文件夹,每个子文件夹都是一个自己创建的应用,应用程序文件夹下包括:应用程序.app文件,Documens目录,tmp目录,Library目录.
但在iphone真机上的目录有所不同:应用程序.app文件位于./Applications/目录下,所有应用程序可读写的目录是./var/mobile/目录,一般要在这个目录下再创建应用程序自己的子目录以读写数据.
3.获取Documents目录:使用Foundation函数NSSearchPathForDirectoriesInDomains.
NSArray* pathArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString* documentsPath=[pathArray objectAtIndex:0];
另外,NSHomeDirectory()返回应用的主目录,在模拟器上返回/Library/Application Surpport/iPhone Simulator/3.1.2/Applications/应用程序文件夹/ ,在真机上返回./var/mobile/
获取tmp目录:
NSString* tmpPath=NSTemporaryDirectory();
4.判断文件路径是否存在
if( [[NSFileManager defaultManager] fileExistsAtPath:filePath] )
5.支持属性列表序列化的类有:
NSArray,NSDictionary,NSData,NSString,NSNumber,NSDate.
NSString* str=[NSString stringWithContentsOfFile:filePathName encoding:NSUTF8StringEncoding error:nil];
[str writeToFile:filePathName atomically:YES encoding:NSUTF8StringEncoding error:nil];
writeToFile要先保证路径中的文件夹都存在,它不会自动创建文件夹。
7.[NSUserDefaults standardUserDefaults]
默认文件存储在应用程序沙盒内部的Library/Preferences中,调用synchronize将立即更新这些默认值。存储为 com.sadun.${EXECUTABLE_NAME}.plist,com.sadun.${EXECUTABLE_NAME}是在Info.plist中设置的Bundle identifier的值。
8. .归档
NSKeyedArchiver
NSKeyedUnarchiver
要编码的对象,必须实现NSCoding协议。
NSCoding协议声明了两个方法,一个方法将对象编码到归档中,另一个对归档解码来创建一个新对象.
NSCopying协议声明了一个用于复制对象的方法.
在自己的对象中实现 NSCoding协议,
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:url_ forKey:@"url"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if(self=[super init])
{
if ([aDecoder containsValueForKey:@"url"])
{
url_=[[aDecoder decodeObjectForKey:@"url"] copy];
}
}
return self;
}
外化对象数组
[NSKeyedArchiver archiveRootObject:array toFile:path];
内化对象数组
array= [[NSKeyedUnarchiver unarchiveObjectWithFile:path] retain];
http://o0o0o0o.iteye.com/blog/588889
9.NSUndoManager 撤销管理器
Undo动作有两种方式:registerUndoWithTarget:selector:object: 和 prepareWithInvocationTarget。前者发送带有一个参数的消息,后者使用NSInvocation (用于对象间存储和转发消息),它可以有任意参数。
处理较大对象时,最好限制级别。
10.Core Data框架提供了持久数据解决方案。它提供了一种灵活的对象管理基础架构,
Core Data 使用内置的 SQLite 数据库,不需要单独安装数据库系统。
http://www.apple.com.cn/developer/technologies/ios/data-management.html
*.sqlite文件会存放在Documents文件夹中。
cd /Users/gzty1/Library/Application\ Support/iPhone\ Simulator/4.2/Applications/9963B7B5-ACAA-473E-888E-F08555BF130A\Documents,执行sqlite *.sqlite;.dump *.sqlite来检查sqlite文件。
11.info.plist中也可以添加自定义的键值。
如果上传到appstore时,报info.plist格式错误,在xml中检查中检查其是否有非法字符。
12.可以使用剪贴板实现应用程序之中以及应用程序之间实现数据的共享
http://www.cocoachina.com/iphonedev/sdk/2011/1209/3700.html
13.sqlite
FMDB
http://blog.csdn.net/f520131480315/article/details/6444297
https://github.com/ccgus/fmdb
下载解压后,可直接使用src目录中的源文件,并添加libsqlite3.0.dylib。
在FMDatabaseAdditions.m中添加 #import <unistd.h>,解决编译报 implicit declaration of function 'usleep' 警告的问题。
14.使用UIGraphicsBeginPDFPage等方法,将UIView保存到NSData中作为pdf文件数据。
15.在caches文件夹中的图片缓存目录
//Applications/CD455B1D-8477-4BC3-89CE-882CDF96E15B/Library/Caches/ImageCache
+ (NSString *)cachePathWithFileName:(NSString*)filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* diskCachePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"];
//保证文件路径存在
if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
}
//对文件名做了转换
const char *str = [filename UTF8String];
unsigned char r[CC_MD5_DIGEST_LENGTH]; CC_MD5(str, (CC_LONG)strlen(str), r);
filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]];
return [diskCachePath stringByAppendingPathComponent:filename];
}
16.NSBundle,读取和解析properties文件
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
参数 tableName :The receiver’s string table to search. If tableName is nil or is an empty string, the method attempts to use the table in Localizable.strings.
tableName指定搜索的.strings文件名,tableName中不要带.strings后缀。
可以在Documents目录下存放要加载的目录文件,例如 skin/night/config.strings
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory =[paths objectAtIndex:0];
NSString* bundlePath=[documentsDirectory stringByAppendingPathComponent:@"skin/night"];
bundle_=[[NSBundle bundleWithPath:bundlePath] retain];
NSString* value=[bundle_ localizedStringForKey:@"nav.title.color" value:@"" table:@"config"];
config.strings文件中的key区分大小写,可以不加双引号,值必须加双引号,config.strings文件名不区分大小写,例如 nav.title.color="#ffffff";
/Users/<youruser>/Library/Application Support/iPhone Simulator/4.3.2/Applications/<yourappguid>/Documents
对于一个运行在iPhone得app,它只能访问自己根目录下得一些文件(即sandbox).
详见apple文档:http://www.apple.com.cn/developer/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/FilesandNetworking/FilesandNetworking.html
参考 http://www.cnblogs.com/drc606/archive/2011/12/18/iosfile.html
常量:
NSDocumentDirectory 存放目录:<Application_Home>/Documents
NSCachesDirectory <Application_Home>/Library/Caches
NSApplicationSupportDirectory <Application_Home>/Library/Application Support
NSUserDefaults <Application_Home>/Library/Preferences/<your App's bundle ID>.plist
1.应用程序根目录:NSDocumentDirectory
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsPath = [paths objectAtIndex:0];
NSString* documentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString* documentsFilePath = [documentsPath stringByAppendingPathComponent:@"filename1.txt"];
2.应用程序目录下的缓存目录:NSCachesDirectory,Location of discardable cache files (/Library/Caches).
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* diskCachePath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"] retain];
3.临时目录NSTemporaryDirectory,它与[NSHomeDictionary() stringByAppendingPathComponent:@“tmp”]不同,tmp目录在里面写入一些程序运行时需要用得数据,里面写入得数据在程序退出后会没有.
+ (NSString*)tempPathWithFileName:(NSString*)filename
{
NSString* tempPath = NSTemporaryDirectory();
return [tempPath stringByAppendingPathComponent:filename];
}
The tmp dir is cleared each 3 days
The Caches directory is where you store cache files and other temporary data that your application can re-create as needed. This directory is located inside the Library directory.
Never store files at the top level of this directory: Always put them in a subdirectory named for your application or company.Your application is resp*****ible for cleaning out cache data files when they are no longer needed.
The system does not delete files from this directory.
To get the path to this directory use the NSCachesDirectory search path key with the NSUserDomainMask domain.
4.创建目录
if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath
withIntermediateDirectories:YES
attributes:nil
error:NULL];
}
5.资源
NSString* path=[[NSBundle mainBundle ] pathForResource:@"default" ofType:@"html"];
6. IOS文件系统及其相关操作(NSFileManager,NSFileHandle)
http://hi.baidu.com/lrorolove/blog/item/578afff57eac83c4f3d3857b.html
7.iOS中使用c函数快速获取一个目录所占的空间大小
/article/3932976.html
1.三种高级数据持久方式:属性列表,对象归档,嵌入式关系数据库SQLite3.
2.应用程序的沙盒:
每个应用程序都有自己的Documents文件夹,并且仅能读取各自的Documents目录中的内容.
/Library/Application Surpport/iPhone Simulator/3.1.2/Applications/目录下有Xcode生成的一些随机名字的文件夹,每个子文件夹都是一个自己创建的应用,应用程序文件夹下包括:应用程序.app文件,Documens目录,tmp目录,Library目录.
但在iphone真机上的目录有所不同:应用程序.app文件位于./Applications/目录下,所有应用程序可读写的目录是./var/mobile/目录,一般要在这个目录下再创建应用程序自己的子目录以读写数据.
3.获取Documents目录:使用Foundation函数NSSearchPathForDirectoriesInDomains.
NSArray* pathArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString* documentsPath=[pathArray objectAtIndex:0];
另外,NSHomeDirectory()返回应用的主目录,在模拟器上返回/Library/Application Surpport/iPhone Simulator/3.1.2/Applications/应用程序文件夹/ ,在真机上返回./var/mobile/
获取tmp目录:
NSString* tmpPath=NSTemporaryDirectory();
4.判断文件路径是否存在
if( [[NSFileManager defaultManager] fileExistsAtPath:filePath] )
5.支持属性列表序列化的类有:
NSArray,NSDictionary,NSData,NSString,NSNumber,NSDate.
NSString* str=[NSString stringWithContentsOfFile:filePathName encoding:NSUTF8StringEncoding error:nil];
[str writeToFile:filePathName atomically:YES encoding:NSUTF8StringEncoding error:nil];
writeToFile要先保证路径中的文件夹都存在,它不会自动创建文件夹。
7.[NSUserDefaults standardUserDefaults]
默认文件存储在应用程序沙盒内部的Library/Preferences中,调用synchronize将立即更新这些默认值。存储为 com.sadun.${EXECUTABLE_NAME}.plist,com.sadun.${EXECUTABLE_NAME}是在Info.plist中设置的Bundle identifier的值。
8. .归档
NSKeyedArchiver
NSKeyedUnarchiver
要编码的对象,必须实现NSCoding协议。
NSCoding协议声明了两个方法,一个方法将对象编码到归档中,另一个对归档解码来创建一个新对象.
NSCopying协议声明了一个用于复制对象的方法.
在自己的对象中实现 NSCoding协议,
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:url_ forKey:@"url"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if(self=[super init])
{
if ([aDecoder containsValueForKey:@"url"])
{
url_=[[aDecoder decodeObjectForKey:@"url"] copy];
}
}
return self;
}
外化对象数组
[NSKeyedArchiver archiveRootObject:array toFile:path];
内化对象数组
array= [[NSKeyedUnarchiver unarchiveObjectWithFile:path] retain];
http://o0o0o0o.iteye.com/blog/588889
9.NSUndoManager 撤销管理器
Undo动作有两种方式:registerUndoWithTarget:selector:object: 和 prepareWithInvocationTarget。前者发送带有一个参数的消息,后者使用NSInvocation (用于对象间存储和转发消息),它可以有任意参数。
处理较大对象时,最好限制级别。
10.Core Data框架提供了持久数据解决方案。它提供了一种灵活的对象管理基础架构,
Core Data 使用内置的 SQLite 数据库,不需要单独安装数据库系统。
http://www.apple.com.cn/developer/technologies/ios/data-management.html
*.sqlite文件会存放在Documents文件夹中。
cd /Users/gzty1/Library/Application\ Support/iPhone\ Simulator/4.2/Applications/9963B7B5-ACAA-473E-888E-F08555BF130A\Documents,执行sqlite *.sqlite;.dump *.sqlite来检查sqlite文件。
11.info.plist中也可以添加自定义的键值。
如果上传到appstore时,报info.plist格式错误,在xml中检查中检查其是否有非法字符。
12.可以使用剪贴板实现应用程序之中以及应用程序之间实现数据的共享
http://www.cocoachina.com/iphonedev/sdk/2011/1209/3700.html
13.sqlite
FMDB
http://blog.csdn.net/f520131480315/article/details/6444297
https://github.com/ccgus/fmdb
下载解压后,可直接使用src目录中的源文件,并添加libsqlite3.0.dylib。
在FMDatabaseAdditions.m中添加 #import <unistd.h>,解决编译报 implicit declaration of function 'usleep' 警告的问题。
14.使用UIGraphicsBeginPDFPage等方法,将UIView保存到NSData中作为pdf文件数据。
15.在caches文件夹中的图片缓存目录
//Applications/CD455B1D-8477-4BC3-89CE-882CDF96E15B/Library/Caches/ImageCache
+ (NSString *)cachePathWithFileName:(NSString*)filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString* diskCachePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"];
//保证文件路径存在
if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath])
{
[[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
}
//对文件名做了转换
const char *str = [filename UTF8String];
unsigned char r[CC_MD5_DIGEST_LENGTH]; CC_MD5(str, (CC_LONG)strlen(str), r);
filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10], r[11], r[12], r[13], r[14], r[15]];
return [diskCachePath stringByAppendingPathComponent:filename];
}
16.NSBundle,读取和解析properties文件
- (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
参数 tableName :The receiver’s string table to search. If tableName is nil or is an empty string, the method attempts to use the table in Localizable.strings.
tableName指定搜索的.strings文件名,tableName中不要带.strings后缀。
可以在Documents目录下存放要加载的目录文件,例如 skin/night/config.strings
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *documentsDirectory =[paths objectAtIndex:0];
NSString* bundlePath=[documentsDirectory stringByAppendingPathComponent:@"skin/night"];
bundle_=[[NSBundle bundleWithPath:bundlePath] retain];
NSString* value=[bundle_ localizedStringForKey:@"nav.title.color" value:@"" table:@"config"];
config.strings文件中的key区分大小写,可以不加双引号,值必须加双引号,config.strings文件名不区分大小写,例如 nav.title.color="#ffffff";
相关文章推荐
- 数据持久,数据共享,NSFileManager,NSFileHandler,NSBundle
- iOS NSFileManager共享数据的坑
- 数据持久,数据共享
- linux 共享内存shm_open实现进程间大数据交互
- 利用Application共享全局数据
- 使用ContentProvider实现数据共享
- 用react-redux实现react组件之间数据共享的方法
- iOS中数据持久存储方式及区别
- 深入浅析JavaScript中数据共享和数据传递
- C++并发编程2——为共享数据加锁(二)
- 使用 PHP 直接在共享内存中存储数据集
- 线程范围内共享数据(二)
- iOS中常用数据持久化总结
- 数据流量监测工具源玛共享3[带录制功能]
- 基于XML的数据共享
- 实现数据共享必须打破部门墙(转)
- 后台定位提供经纬度,向多个Activity共享数据
- 多个Google账户合并(共享)数据
- 多个线程访问共享对象和数据的方式
- 第一行代码 第7章 内容提供器 --实现跨程序数据共享