1.常用路径的获取方式
/**
常用路径获取方式
*/
- (void)filePath {
//应用程序包目录:存放应用程序的源文件,包括源文件和可执行文件
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSLog(@"bundlePath === %@",bundlePath);
//Documents:最常用的目录, iTunes 同步该应用时会同步此文件夹中的内容,适合存储重要数据
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSLog(@"documentsPath === %@",documentsPath);
//Library / Cache 目录: iTunes 不会同步此文件夹,适合存储体积较大,不需要备份的非重要数据
NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
NSLog(@"libraryPath === %@",libraryPath);
//temp 目录: iTunes 不会同步此文件夹,用于保存临时文件夹
NSString *tempPath = NSTemporaryDirectory();
NSLog(@"tempPath === %@",tempPath);
}
2.通过 plist 文件存储数据
/**
通过 plist 文件存储数据
*/
- (void)saveByPlist {
//plist 文件内可以写入的数据类型包括:NSArray,NSMutableArray,NSDictionary,NSMutableDictionary,NSData,NSMutableData,NSString,NSMutableString,NSNumber,NSDate.
//获取路径
NSString *plistPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
//创建 plist 文件
NSString *filePath = [plistPath stringByAppendingString:@"myPlistFile.plist"];
NSArray *saveArr = @[@1,@2,@3];
//写入数据
[saveArr writeToFile:filePath atomically:YES]; //atomically设置为 YES 的含义是先生成一个辅助文件,然后将辅助文件拷贝至目标路径,这种写法更安全.
//读取数据
NSLog(@"%@",[NSArray arrayWithContentsOfFile:filePath]);
}
3.通过 NSUserDefault 存储数据
/**
通过 NSUserDefault 存储数据
*/
- (void)saveByNSUserDefault {
//NSUserDefault 是全局单例的,所有的数据均存储到系统 preference 目录下的一个以此应用包名称命名的 plist 文件内.
NSUserDefaults *myUserDefault = [NSUserDefaults standardUserDefaults];
[myUserDefault setObject:@"test" forKey:@"userDefault"];
[myUserDefault synchronize];
NSLog(@"%@",[myUserDefault objectForKey:@"userDefault"]);
}
4.通过归档与解档来存储数据
需要实现<NSCoding>协议
#import <Foundation/Foundation.h>
@interface MyCodingSave : NSObject
//声明属性
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
#import "MyCodingSave.h"
@interface MyCodingSave ()<NSCoding>
@end
@implementation MyCodingSave
//归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
//解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self == [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
@end
/**
�归档解档实现数据存储
*/
- (void)saveByCoding {
//路径
NSString *plistPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
//文件扩展名不一定为 .data 格式,可以任意指定,不建议使用中文后缀
NSString *filePath = [plistPath stringByAppendingString:@"myCoding.data"];
//归档
MyCodingSave *coding = [[MyCodingSave alloc] init];
coding.name = @"djlovettt";
coding.age = 27;
[NSKeyedArchiver archiveRootObject:coding toFile:filePath];
//解档
MyCodingSave *result = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
if (result) {
NSLog(@"name === %@",result.name);
NSLog(@"age === %ld",(long)result.age);
}
}
5.通过数据库实现数据存储
FMDB / MagicalRecord 等方案