美文网首页IOS文件
iOS开发--数据持久化

iOS开发--数据持久化

作者: JohnnyB0Y | 来源:发表于2015-08-24 16:32 被阅读806次

这是我个人的学习笔记 , 如有不同见解欢迎评论交流 .
( 我的微博 : http://weibo.com/JohnnyB0Y )

  • 沙盒机制

1.每个应用都有自己的沙盒,不能越界读写其他应用的数据。
2.获取Documents文件夹路径
NSArray *pathsList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *documentPath = [pathsList lastObject];
3.获取Library文件夹路径
NSArray *pathsList = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask,
YES);
NSString *libraryPath = [pathsList lastObject];
4.获取tmp文件夹路径
NSString *tmpPath = NSTemporaryDirectory();

沙盒文件夹 使用场景
Documents iTunes会同步,存放重要的数据,希望同步到其他设备的数据。
Library iTunes不会同步,设置程序的默认设置和其他状态信息。
Library/Caches 缓存文件夹,保存可以从网络上重复获取的资源(节省流量)。
Library/Preferences 保存一些用户信息配置文件,LaunchScreen生成的启动图片。
tmp 创建临时文件的目录,当iOS设备重启时,文件会被自动清除。
  • NSFileManager

1.创建一个文件并写入数据
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
2.从一个文件中读取数据
- (NSData *)contentsAtPath:(NSString *)path;
3.scrPath路径上的文件移动到dstPath路径上,注意这里的路径是文件路径而不是目录
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **) error;
4.scrPath路径上的文件复制到dstPath路径上
- (BOOL)copyItemAtPath:(NSString *)scrPath toPath:(NSString *)dstPath error:(NSError **) error;
5.比较两个文件的内容是否一样
- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2;
6.文件是否存在
- (BOOL)fileExistsAtPath:(NSString *)path;
7.移除文件
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **) error;
8.创建文件管理
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [NSHomeDirectory( ) stringByAppendingPathComponent:@"holyBible.txt"];
NSString *text = @"abcdefg";
9.将字符串转成NSData类型
NSData *data = [text dataUsingEncoding: NSUTF8StringEncoding];
10.写入文件
BOOL success = [fileManager createFileAtPath:path contents:data attributes:nil];
11.创建文件夹
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSString *contect = @"abcdefg”;
BOOL success = [fm createFileAtPath:filePath contents:[content dataUsingEncoding: NSUTF8StringEncoding] attributes:nil];
12.NSFileManager-读取内容
NSData *fileData = [fileManager contentsAtPath:filePath];
NSString *content = [[NSString alloc] initWithData:fileData dataUsingEncoding: NSUTF8StringEncoding];
13.NSData-读取内容
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSData *data = [NSData dataWithContentOfFile:filePath];
13.1 NSString-读取内容
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSString *content = [[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
移动、复制文件
14.移动文件(重命名)
NSString *toPath = [NSHomeDirectory( ) stringByAppendingPathComponent:@"hellogod/New Testament.txt"];
[fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
NSError *error;
BOOL isSuccess = [fm moveItemAtPath:filePath toPath:toPath error:&error];
15.复制文件(重命名)
NSString *copyPath = [NSHomeDirectory( ) stringByAppendingPathComponent:@"备份/Old Testament.txt"];
[fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
BOOL success = [fm copyItemAtPath:toPath toPath:toPath error:nil];
删除文件、获取文件大小
16.判断文件是否存在和删除文件
if([fm fileExistsAtPath])
{
if ([fm removeItemAtPath:copyPath])
{
NSLog(@"remove success");
}
}
17.获得文件的属性字典
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *attrDic = [fileManager attributesOfItemAtpath:sourcePath error:nil];
18.获取文件大小
NSNumber *fileSize = [attrDic objectForKey:NSFileSize];
19.获取目录文件信息
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *enuPath = [NSHomeDirectoty( ) stringByAppendingPathComponent:@"Test"];
NSDictionaryEnumerator *dirEnum = [fileManager enumeratorAtPath:enuPath];
NSString *path = nil;
while ((path = [dirEnum nextObject]} != nil){
NSLog(@"%@",path);

  • Plist、Archive数据归档

#######Plist就是数组、字典(xml的组织形式)直接写到沙盒中。
字典:[dictionary writeToFile:写入文件的路径 atomically:是否原子写入];
数组:[array writeToFile:写入文件的路径 atomically:是否原子写入];
#######Archive归档解档。
1.要归档的类需要实现<NSCoding>协议
1.1 解档属性:重写
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.requestData = [aDecoder decodeObjectForKey:@"requestData"];
}
return self;
}

  1.2 归档属性:重写
  - (void)encodeWithCoder:(NSCoder *)aCoder
  {
      [aCoder encodeObject:_requestData forKey:@"requestData"];
  }

2.对类完整归档解档
NSString *objKey = @"objKey";
NSString *savePath = [NSString stringWithFormat:@"%@/save.data", NSTemporaryDirectory()];
// 1 归档
NSMutableData *dataM = [NSMutableData data];
NSKeyedArchiver *keyedArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:dataM];
[keyedArchiver encodeObject:@"对象" forKey:objKey];
[keyedArchiver finishEncoding];
// 归档后把dataM保存到沙盒
[dataM writeToFile:savePath atomically:YES];

  // 2 解档
  // 从沙盒读取data
  NSData *data = [[NSData alloc] initWithContentsOfFile:savePath];
  // 开始解档
  NSKeyedUnarchiver *keyedUnarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  NSString *string = [keyedUnarchiver decodeObjectForKey:objKey];
  [keyedUnarchiver finishDecoding];
  • NSUserDefaults

1.属于简化版的Archive归解档
NSString *objKey = @"objKey";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// 保存数据(其中有很多类型的数据)
[userDefaults setObject:@"对象" forKey:objKey];
// 读取数据
[userDefaults objectForKey:objKey];

  • SQLite3应用

iOS开发--SQLite数据库

相关文章

  • iOS本地数据持久化

    iOS本地数据持久化 iOS本地数据持久化

  • 数据持久化基础知识

    参考: iOS开发中的4种数据持久化方式【一、属性列表与归档解档】 iOS开发中的4种数据持久化方式【二、数据库 ...

  • iOS开发-数据持久化之plist文件

    摘要 通过对plist文件的操作对iOS开发中一些数据进行持久化保存。 iOS数据持久化之一——plist文件 i...

  • iOS本地数据持久化

    转载自:CocoaChina - iOS本地数据持久化 本文内容:iOS本地数据持久化的几种类型iOS本地数据持久...

  • iOS 开发技术选型之数据库:SQLite vs. Core D

    持久化方案 在 iOS 开发中,数据持久化存储是一个很常见的需求。所谓持久化存储,就是将数据存到硬盘,使得应用重启...

  • iOS持久化方式有哪些

    首先这里的持久化指的是数据持久化,目前客户端的持久化也只有这一个含义。为何要持久化:iOS开发可以没有持久化,持久...

  • 【code_hyy_基础】iOS持久化方式

    首先这里的持久化指的是数据持久化,目前客户端的持久化也只有这一个含义。 为何要持久化: iOS开发可以没有持久化,...

  • iOS 数据持久化方案-Realm的使用

    iOS 数据持久化方案-Realm的使用 iOS 数据持久化方案-Realm的使用

  • iOS本地数据持久化

    本文内容: iOS本地数据持久化的几种类型 iOS本地数据持久化几种类型的应用场景及使用 一.iOS本地数据持久化...

  • NSKeyedArchiver简述

    1.1关于数据的持久化存储的几种方式 说到NSKeyedArchiver,也就先要了解下iOS开发中关于数据持久...

网友评论

  • 编号x71291:NSUserDefaults保存数据在哪里呢
    JohnnyB0Y:@新宛 保存在Library 的Perferences文件夹里。你可以先使用,然后打开文件夹看看!

本文标题:iOS开发--数据持久化

本文链接:https://www.haomeiwen.com/subject/tgafcttx.html