标签:
//应用程序沙盒主目录获取
NSString *home = NSHomeDirectory();
//应用程序tmp文件夹
NSString *tmp = NSTemporaryDirectory();
//应用程序Documents目录获取
NSString *document = [home stringByAppendingPathComponent:@"Documents"];
NSString *document1 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//获取应用程序Library路径
NSString *library = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
//获取应用程序Library/Caches路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//存储
NSString *document = [home stringByAppendingPathComponent:@"Documents"];
NSString *path = [document stringByAppendingPathComponent:@"aaa.plist"];
NSArray *array = @[@"aaa",@"bbb",@"ccc"];
[array writeToFile:path atomically:YES];
path = [tmp stringByAppendingPathComponent:@"bbb.plist"];
NSDictionary *dict = @{@"name":@"zhangct", @"age":@"28"};
[dict writeToFile:path atomically:YES];
//读取
NSString *tmp = NSTemporaryDirectory();
NSString *path = [tmp stringByAppendingPathComponent:@"bbb.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [document stringByAppendingPathComponent:@"aaa.plist"];
//存储
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"zhangct" forKey:@"name"];
[defaults setInteger:35 forKey:@"age"];
[defaults setBool:YES forKey:@"sex"];
[defaults synchronize]; //立刻同步?
//读取
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *name = [defaults valueForKey:@"name"];
NSInteger age = [defaults integerForKey:@"age"];
BOOL sex = [defaults boolForKey:@"sex"];
@interface CTPerson : NSObject<NSCoding>
@end
@implementation CTPerson
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.name forKey:@"name"];
[coder encodeInteger:self.age forKey:@"age"];
[coder encodeFloat:self.height forKey:@"height"];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
if (self = [super init]) {
self.name = [coder decodeObjectForKey:@"name"];
self.age = [coder decodeIntegerForKey:@"age"];
self.height = [coder decodeFloatForKey:@"height"];
}
return self;
}
@end
@implementation CTStudent
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeInteger:self.score forKey:@"score"];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
if (self = [super initWithCoder:coder]) {
self.score = [coder decodeIntegerForKey:@"score"];
}
return self;
}
#define PATH [NSHomeDirectory() stringByAppendingPathComponent:@"student.arc"];
//归档
NSString *path = PATH;
CTStudent *stu = [[CTStudent alloc] init];
stu.name = @"zhangct";
stu.age = 35;
stu.height = 1.72;
stu.score = 89;
[NSKeyedArchiver archiveRootObject:stu toFile:path];
//解档
CTStudent *stu = [[CTStudent alloc] init];
NSString *path = PATH;
stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
标签:
原文地址:http://www.cnblogs.com/linxiu-0925/p/5058169.html