标签:additional 应用程序 interface loading import
#import "MainViewController.h"
#import "Student.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//数据持久化 ,本地存贮
//1.通过代码找到当前应用程序的沙盒路径
//函数: 参数1:要找沙盒中的哪个文件夹
//参数2:在系统的什么地方去找文件夹(在IOS系统下,只能用NSUserDomainMask = 1)
//参数3:是否是一个绝对路径
// NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//
// NSLog(@"documents的路径:%@",paths);
//
//三个文件夹的作用
//1.Documents文件夹:存贮用户的设置,用户需要保存的一些数据
//限制:如果你在iCloud服务,会把Documents文件中所有的数据都上传到服务器上.iCloud服务器提供空间是有限的(5G),不要贼这个文件夹存储音频/视频/体积比较大的问价;
//2.Library: --Caches;缓存文件夹,存贮系统/用户的一些缓存文件(比如:txt/图片/视频/音频...,清楚缓存就是删除这个文件夹)
//Perferences文件夹:是给开发者使用的偏好设置文件夹,用来存贮应用执行需要的一些数据.
//3.tmp文件夹:存贮临时文件 ,在版本更新的时候会被删除掉所有内容.
//4.应用程序的可执行文件(.app),包含这个应用程序执行的所有代码,资源,基本设置,(完全不能被修改)(NSBundle)
//获取app中一个图片路径
//参数1:资源的文件名
//参数2:资源的文件类型
// NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"1-121126113549" ofType:@"jpg"];
// NSLog(@"%@",imagePath);
//
// UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
//两种显示图片的方式;
// UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
// UIImage *image1 = [UIImage imageNamed:@"1-121126113549.jpg"];
// imageView.image = [UIImage imageWithContentsOfFile:imagePath];
// [self.view addSubview:imageView];
//imageNamed加载图片,并且把image缓存到内存里面,
//imageWithContentsOfFile是只显示图片,但不加到内存中。
//所以加载大量图片的时候用imageWithContentsOfFile比较好,内存不会变大。
//--------------简单的对象写入本地--_-_------------//
//NSString,NSArray,NSDictionary,,,这种系统自己提供数据容器类产生的对象,称为简单对象
//NSString字符串写入本地
NSString *string = @"没事儿不要老请假!";
//系统提供的直接写入本地的方法
//拼接文件路径
//获取libary的文件夹路径
NSString *libPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];
NSString *strPath = [libPath stringByAppendingString:@"/string.txt"];
NSLog(@"字符串文件的路径%@",strPath);
//参数1:文件的路径 参数2:是否对存贮的过程进行保护 参数3:对文本的编码方式(utf8) 参数4:错误信息()
NSError *error = nil;
[string writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
NSLog(@"错误信息: %@",error);
}
//从本地读取文件数据
NSString *readStr = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", readStr);
//数组的读写
NSArray *array = [NSArray arrayWithObjects:@"aa",@"bb", @"cc", @"dd", @"ee", @"ff", nil];
//数组对象的路径拼接
NSString *arrayPath = [libPath stringByAppendingPathComponent:@"array.plist"];
[array writeToFile:arrayPath atomically:YES];
//读取
NSArray *readArray = [NSArray arrayWithContentsOfFile:arrayPath];
NSLog(@"%@",readArray);
//使用NSUserDefaults类进行本地存储
//这是一个可以保存在本地的系统的字典
//使用一个单例方法 创建这个对象
//单例 : 设计模式:在一个程序运行中,只会产生一个对象
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
//使用
[userdefaults setObject:@"alksdjflkasd" forKey:@"key"];
//立即写入本地
[userdefaults synchronize];
//显示 (判断第一次登陆)
NSLog(@"%@", [userdefaults objectForKey:@"key"]);
//判断用户是否是第一次使用应用
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"isFirstLogin"] == nil) {
//如果第一次使用
[[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"isFirstLogin"];
[[NSUserDefaults standardUserDefaults] synchronize];
}else{
//不是第一次
}
//4.复杂对象写入本地
Student *stu = [[Student alloc] init];
stu.name = @"maidong";
stu.sex = @"nv";
stu.address = @"liaoningdalian";
//归档类 ,把一个复杂对象写入本地(只能实现有NSCoding协议的)
//参数1:你要存储的对象(必须是实现<NSCoding>协议)
//参数2:存储路径
NSString *stuPath = [libPath stringByAppendingString:@"student.suiyi"];
[NSKeyedArchiver archiveRootObject:stu toFile:stuPath];
[stu release];
//读取
//反归档类
Student *stuRead = [NSKeyedUnarchiver unarchiveObjectWithFile:stuPath];
NSLog(@"姓名:%@,性别:%@,住址:%@",stuRead.name,stuRead.sex,stuRead.address);
//存储复杂对象的流程
//1.建立类,实现NSCoding协议,对所有的属性进行编码的解码
//2.利用归档类(NSKeyedArchinver)对一个类的对象进行归档写入本地
//3利用反归档类(NSKeyedUnarchiver)对一个路径下的归档文件进行反归档,产生一个新的对象
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end本文出自 “小刘_Blog” 博客,请务必保留此出处http://liuyafang.blog.51cto.com/8837978/1553016
标签:additional 应用程序 interface loading import
原文地址:http://liuyafang.blog.51cto.com/8837978/1553016