数据持久,数据共享
2010-12-07 09:17
169 查看
1.四种高级数据持久方式:属性列表,对象归档,嵌入式关系数据库SQLite3、coredata.
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];
7.[NSUserDefaults standardUserDefaults]
默认文件存储在应用程序沙盒内部的Library/Preferences中,调用synchronize将立即更新这些默认值。存储为 com.sadun.${EXECUTABLE_NAME}.plist,com.sadun.${EXECUTABLE_NAME}是在Info.plist中设置的Bundle identifier的值。
NSUserDefaults用于保存iPhone程序的数据,存放于一个plist文件中,位于 <UUID for your App>/Library/Preferences/<your App's bundle ID>.plist
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文件数据。
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];
7.[NSUserDefaults standardUserDefaults]
默认文件存储在应用程序沙盒内部的Library/Preferences中,调用synchronize将立即更新这些默认值。存储为 com.sadun.${EXECUTABLE_NAME}.plist,com.sadun.${EXECUTABLE_NAME}是在Info.plist中设置的Bundle identifier的值。
NSUserDefaults用于保存iPhone程序的数据,存放于一个plist文件中,位于 <UUID for your App>/Library/Preferences/<your App's bundle ID>.plist
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文件数据。
相关文章推荐
- 数据持久,数据共享,NSFileManager,NSFileHandler,NSBundle
- 数据持久,数据共享,NSFileManager,NSFileHandler,NSBundle
- Android 数据共享机制! .
- 关于双核通信通过共享内存交换数据ARM端程序的学习
- ContentProvider共享数据(一)
- 线程间无需特别的手段进行通信,因为线程间可以共享数据结构,也就是一个全局变量可以被两个线程同时使用,不过要注意的是线程间需要做好同步。
- 使用参数innodb_file_per_table支持MySQL InnoDB表数据共享空间自动收缩
- 跨程序共享数据,探究内容提供器
- 使用 PHP 直接在共享内存中存储数据集
- [置顶] 跟我学AngularJs:Controller数据共享、继承、通信使用详解
- web应用中三种数据共享方式:request,session,application
- PHP实现多服务器共享SESSION数据-mysql实现
- Android 应用程序之间数据共享—ContentProvider
- 线程范围内的共享数据(一)
- 多线程之——共享数据
- 进程间的数据共享
- 共享数据的锁定(订单系统)
- android ContentProvider简单实现不同应用间的数据共享
- DLL入门浅析(5)——使用DLL在进程间共享数据
- Android进阶#(1/12)Android的构成基石——四大组件_ContentProvider(外共享数据)