参考网址:http://blog.csdn.net/wxq888/article/details/8191076 ios
数据的存储无疑是软件开发中的重要课题。本文给初学者介绍下iphone开发中常见的文件读写 数据库
iOS的文件存储采用的是“沙箱机制”,也就是应用程序只能访问本身的文件目录,每一个应用程序的数据是独立的,就像一个一个的沙箱同样。这种管理方法比windows和原来的塞班那种文件管理方式更适合移动平台。这种方式更安全,在很大程度上避免了流氓软件和垃圾软件的盗窃资料。 windows
查看模拟器应用程序在mac上的存储,就能够了解在iphone上文件是如何组织的。 安全
打开目录/Users/andy/Library/Application Support/iPhone Simulator/5.1/Applications就会看到模拟器上的程序文件夹,你会看到iphone每一个应用程序都有本身的3个目录(Document,Library,tmp) app
Documents存放应用程序的数据。 iphone
Library目录下面还有Preferences和Caches目录,Preferences目录存放应用程序的使用偏好,Caches目录与Documents很相 是能够存放应用程序的数据。 函数
tmp目录供应用程序存储临时文件。 工具
注意,若是你的设置没有设置为查看隐藏目录,你是看不到的,设置显示隐藏文件方法:在终端输入命令:defaults write com.apple.finder AppleShowAllFiles -bool true而后重启下finder。 atom
在应用程序中得到本身的documents目录: spa
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);NSString * documentDirectory = [paths objectAtIndex:0];
在上面的基础上,得到一个完整的文件路径和名字:
NSString * file = [documentDirectory stringByAppendingPathComponent:@"file1.txt"];
这就能够用file来建立,读取,和写入文件。
iOS中数据存储常见的有四种方式: 属性列表、对象归档、ios嵌入式数据库(SQLite3)和Core Data(苹果提供的工具)
一,用属性列表保存数据:
该方法就是针对一些集合类调用writeToFile:atomically方法和initWithContentsOfFile 方法来写入和读取数据。
这些集合类包括:NSArray,NSMutableArray,NSDictionary,NSMutableDictionary,NSData,NSMutableData,NSString,NSMutableString,NSNumber,NSDate。在这里输入代码太麻烦了,没法识别回车换行,因此这个就不在这里举例了。
二,归档方法保存自定义对象:
属性列表方法简单易用,可是使用有局限性,就是没法保存自定义的数据类。要解决这个问题,咱们看归档方法。归档方法实际就是 用 NSKeyedArchiver 对 自定义类进行编解码成 NSMutableData 后,再对NSMutableData实行序列化具体的编解码是由NSCoder实现的。举个例子就比较容易掌握。
例子中咱们保存4个值和一个名字字典。这些名字包括昵称,qq网名,微博网名,省份证名字。自定义类以下:
头文件:
#import <Foundation/Foundation.h>
@interface BIDFourLines : NSObject
@property (copy, nonatomic) NSString *field1;
@property (copy, nonatomic) NSString *field2;
@property (copy, nonatomic) NSString *field3;
@property (copy, nonatomic) NSString *field4;
@property (copy, nonatomic) NSDictionary * names;
实现文件:
#import "BIDFourLines.h"
#define kField1Key @"Field1"
#define kField2Key @"Field2"
#define kField3Key @"Field3"
#define kField4Key @"Field4"
#define kFieldArrayKey @"FieldArray"
#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:field1 forKey:kField1Key];
[encoder encodeObject:field2 forKey:kField2Key];
[encoder encodeObject:field3 forKey:kField3Key];
[encoder encodeObject:field4 forKey:kField4Key];
[encoder encodeObject:names forKey:kFieldArrayKey];
}
- (id)initWithCoder:(NSCoder *)decoder {
if (self = [super init]) {
field1 = [decoder decodeObjectForKey:kField1Key];
field2 = [decoder decodeObjectForKey:kField2Key];
field3 = [decoder decodeObjectForKey:kField3Key];
field4 = [decoder decodeObjectForKey:kField4Key];
names = [decoder decodeObjectForKey:kFieldArrayKey];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone {
BIDFourLines *copy = [[[self class] allocWithZone: zone] init];
copy.field1 = [self.field1 copyWithZone:zone];
copy.field2 = [self.field2 copyWithZone:zone];
copy.field3 = [self.field3 copyWithZone:zone];
copy.field4 = [self.field4 copyWithZone:zone];
copy.names = [self.names copyWithZone:zone];
return copy;
}
保存文件代码:
- (void) saveTofile{
BIDFourLines *fourLines = [[BIDFourLines alloc] init];
fourLines.field1 = field1.text;
fourLines.field2 = field2.text;
fourLines.field3 = field3.text;
fourLines.field4 = field4.text;
fourLines.names = [NSDictionary dictionaryWithObjectsAndKeys:@"andy",@"nickName",@"王勃",@"idName",@"田鼠",@"qqName",@"大力哥",@"weiboName",nil ]; NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:fourLines forKey:kDataKey];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
}
获取文件名函数:
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"archive"];
}
- (void) loadFileData{
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
BIDFourLines *fourLines = [unarchiver decodeObjectForKey:kDataKey];
[unarchiver finishDecoding];
field1.text = fourLines.field1;
field2.text = fourLines.field2;
field3.text = fourLines.field3;
field4.text = fourLines.field4;
NSDictionary * names = fourLines.names;
if(names) {
NSArray * array = [names allKeys];
for (NSString * value in array) {
NSLog(@"%@ is %@",value,[names objectForKey:value]);
}
}
}
}