标签:
模拟器应用沙盒的根路径在: (apple是用户名, 6.0是模拟器版本)
/Users/apple/Library/Application Support/iPhone Simulator/6.0/Applications
iOS应用数据存储的常用方式:
// 存储数据 - (void)savePlist { // 1.获得沙盒根路径 NSString *home = NSHomeDirectory(); // 2.document路径 NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; // 3.新建数据 NSArray *data = @[@"jack", @10, @"ffdsf"]; NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"]; [data writeToFile:filepath atomically:YES]; } // 读取数据 - (void)readPlist { // 1.获得沙盒根路径 NSString *home = NSHomeDirectory(); // 2.document路径 NSString *docPath = [home stringByAppendingPathComponent:@"Documents"]; // 3.文件路径 NSString *filepath = [docPath stringByAppendingPathComponent:@"data.plist"]; // 4.读取数据 NSArray *data = [NSArray arrayWithContentsOfFile:filepath]; NSLog(@"%@", data); }
// 存储 - (IBAction)save { // 1.利用NSUserDefaults,就能直接访问软件的偏好设置(Library/Preferences) NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // 2.存储数据 [defaults setObject:@"mj" forKey:@"account"]; [defaults setObject:@"123" forKey:@"pwd"]; [defaults setInteger:10 forKey:@"age"]; [defaults setBool:YES forKey:@"auto_login"]; // 3.立刻同步 [defaults synchronize]; } // 读取 - (IBAction)read { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *account = [defaults objectForKey:@"account"]; BOOL autoLogin = [defaults boolForKey:@"auto_login"]; NSLog(@"%@ -- %d", account, autoLogin); }
[defaults synchornize];
MJStudent.h
#import <Foundation/Foundation.h> @interface MJStudent : NSObject <NSCoding> @property (nonatomic, copy) NSString *no; @property (nonatomic, assign) double height; @property (nonatomic, assign) int age; @end
MJStudent.m
#import "MJStudent.h" @interface MJStudent() @end @implementation MJStudent /** * 将某个对象写入文件时会调用 * 在这个方法中说清楚哪些属性需要存储 */ - (void)encodeWithCoder:(NSCoder *)encoder { [encoder encodeObject:self.no forKey:@"no"]; [encoder encodeInt:self.age forKey:@"age"]; [encoder encodeDouble:self.height forKey:@"height"]; } /** * 从文件中解析对象时会调用 * 在这个方法中说清楚哪些属性需要存储 */ - (id)initWithCoder:(NSCoder *)decoder { if (self = [super init]) { // 读取文件的内容 self.no = [decoder decodeObjectForKey:@"no"]; self.age = [decoder decodeIntForKey:@"age"]; self.height = [decoder decodeDoubleForKey:@"height"]; } return self; } @end
控制器使用:
- (IBAction)save { // 1.新的模型对象 MJStudent *stu = [[MJStudent alloc] init]; stu.no = @"42343254"; stu.age = 20; stu.height = 1.55; // 2.归档模型对象 // 2.1.获得Documents的全路径 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // 2.2.获得文件的全路径 NSString *path = [doc stringByAppendingPathComponent:@"stu.data"]; // 2.3.将对象归档 [NSKeyedArchiver archiveRootObject:stu toFile:path]; } - (IBAction)read { // 1.获得Documents的全路径 NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; // 2.获得文件的全路径 NSString *path = [doc stringByAppendingPathComponent:@"stu.data"]; // 3.从文件中读取MJStudent对象 MJStudent *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; NSLog(@"%@ %d %f", stu.no, stu.age, stu.height); }
标签:
原文地址:http://www.cnblogs.com/jys509/p/4828577.html