本文主要讲解的是 NSDate时间、NSUserDefaults本地存储、NSNotification系统通知的详细使用方式,也是NS系列基础知识的最后一篇文章
文章是博主原创,转载请标明出处http://blog.csdn.net/werctzzz/article/details/72677981
NSUserDefaults是iOS系统提供的一个单例类(iOS提供了若干个单例类),通过类方法standardUserDefaults可以获取NSUserDefaults单例。
NSUserDefaults适合存储轻量级的本地数据。存储的数据在关闭程序之后,再次运行的时候依然存在,它的数据存储在/Library/Prefereces沙盒路径下.
// NSUserDefaults支持数据格式:
// NSNumber
// NSString
// NSArray
// NSDictionary
// BOOL
// NSDate
// NSUserDefaults提供的快捷对象存储方式:
// setBool: forKey:
// setFloat: forKey:
// setInteger: forKey:
// setDouble: forKey:
// setURL: forKey:
// 1.初始化:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// 2.NSUserDefaults操作,和字典一样通过key值来存入和读取数据
// 2.1 创建存入的数据
[defaults setInteger:23 forKey:@"myInteger"];
// 2.2 数据存入磁盘
[defaults synchronize];
// ⭐️方法synchronise是为了强制存储,其实并非必要,因为这个方法会在系统中默认调用,但是你确认需要马上就存储,这样做是可行的。
// 2.3 通过key值读取数据
NSLog(@"%ld",[defaults integerForKey:@"myInteger"]);
// 3.因为NSUserDefaults所存储的对象和值是不可变的(只能覆盖)
// 3.1 所以在存可变的类型时候我们需要这样
NSMutableArray *mutableArray1 = [NSMutableArray arrayWithObjects:@"123",@"234", nil];
NSArray * array = [NSArray arrayWithArray:mutableArray1];
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:array forKey:@"Array"];
// 3.2 在读取的时候我们也是需要这样来操作
NSMutableArray *mutableArray2 = [NSMutableArray arrayWithArray:[user objectForKey:@"记住存放的一定是不可变的"]];
// 4.我们可以试试存储图片(上期讲过的NSData)
UIImage *image1 =[UIImage imageNamed:@"aaimg"];
NSData *imageData1 = UIImageJPEGRepresentation(image1, 100);//把image归档为NSData
[user setObject:imageData1 forKey:@"image"];
// 读取
NSData *imageData2 = [user dataForKey:@"image"];
UIImage *image2 = [UIImage imageWithData:imageData2];
引用“iOS 提供了一种 "同步的" 消息通知机制,观察者只要向消息中心注册, 即可接受其他对象发送来的消息,消息发送者和消息接受者两者可以互相一无所知,完全解耦。这种消息通知机制可以应用于任意时间和任何对象,观察者可以有多个,所以消息具有广播的性质,只是需要注意的是,观察者向消息中心注册以后,在不需要接受消息时需要向消息中心注销,这种消息广播机制是典型的“Observer”模式。这个要求其实也很容易实现. 每个运行中的application都有一个NSNotificationCenter的成员变量,它的功能就类似公共栏. 对象注册关注某个确定的notification(如果有人捡到一只小狗,就去告诉我). 我们把这些注册对象叫做 observer. 其它的一些对象会给center发送notifications(我捡到了一只小狗). center将该notifications转发给所有注册对该notification感兴趣的对象. 我们把这些发送notification的对象叫做 poster。消息机制常常用于在向服务器端请求数据或者提交数据的场景,在和服务器端成功交互后,需要处理服务器端返回的数据,或发送响应消息等,就需要用到消息机制。”
- (void)NSNotificationUseFunction{
// 通知模式NSNotification(也称消息中心)
// 1.注册接收的通知者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test:) name:@"testNotification" object:nil];
// 1.1发送通知
[self sendUsername];
// 系统通知
// 2.APP切换到后台
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterBack:) name:UIApplicationDidEnterBackgroundNotification object:nil];
// 3.APP切换回前台
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterFace:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
// 1.1通知发送者
- (void)sendUsername{
// 创建要发送的数据
NSDictionary * data = [NSDictionary dictionaryWithObject:@"我是名字" forKey:@"username"];
// 向中心发送数据
[[NSNotificationCenter defaultCenter]postNotificationName:@"testNotification" object:nil userInfo:data];
}
// 1.2接收通知的内容
- (void)test:(NSNotification*)testNotifi{
NSDictionary * data = [testNotifi userInfo];
NSString * userName = [data objectForKey:@"username"];
NSLog(@"username:%@",userName);
}
// 2.App切换到后台
- (void)enterBack:(NSNotification*)enterBackNotifi{
NSLog(@"进入到后台");
}
// 3.App切换到前台
- (void)enterFace:(NSNotification*)enterFaceNotifi{
NSLog(@"进入到前台");
}
NSDate 是苹果提供的一个类,在iOS开发中用于处理时间和日期
// 获取当前日期
// 1.初始化
NSDate *date = [NSDate date];
NSLog(@"当前时间 date = %@",date);
// 2.改变时间
// 获取从某个日期开始往前或者往后多久的日期,此处60代表60秒,如果需要获取之前的,将60改为-60即可
date = [[NSDate alloc] initWithTimeInterval:60 sinceDate:[NSDate date]];
NSLog(@"当前时间 往后60s的时间date = %@",date);// 可以和上面的打印时间做比较
// 3.当地时间的获取,
// 应为iOS默认是格林尼治时间,所以需要用NSTimeZone矫正时间时区,NSTimeZone会获取本机定位的区域用来获取时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localDate = [date dateByAddingTimeInterval: interval];
NSLog(@"当前地区时间 localDate = %@",localDate);
// 4.时间的格式化
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy"];
NSInteger currentYear=[[formatter stringFromDate:date] integerValue];
[formatter setDateFormat:@"MM"];
NSInteger currentMonth=[[formatter stringFromDate:date]integerValue];
[formatter setDateFormat:@"dd"];
NSInteger currentDay=[[formatter stringFromDate:date] integerValue];
NSLog(@"当前时间 = %@ ,年 = %ld ,月=%ld, 日=%ld",date,currentYear,currentMonth,currentDay);
// 5.NSDate与NSString
NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
// 5.1设置日期格式
[dateFormatter setDateFormat:@"年月日 YYYY/mm/dd 时间 hh:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"dateString = %@",dateString);
// 5.2设置日期格式
[dateFormatter setDateFormat:@"YYYY-MM-dd"];
NSString *year = [dateFormatter stringFromDate:date];
NSLog(@"年月日 year = %@",year);
// 5.3设置时间格式
[dateFormatter setDateFormat:@"hh:mm:ss"];
NSString *time = [dateFormatter stringFromDate:date];
NSLog(@"时间 time = %@",time);
// 可以看看打印结果是什么~
// 6.日期的比较NSDate的比较
// 当前时间
NSDate *currentDate = [NSDate date];
// 比当前时间晚一个小时的时间
NSDate *laterDate = [[NSDate alloc] initWithTimeInterval:60*60 sinceDate:[NSDate date]];
// 比当前时间早一个小时的时间
NSDate *earlierDate = [[NSDate alloc] initWithTimeInterval:-60*60 sinceDate:[NSDate date]];
// 比较哪个时间迟
if ([currentDate laterDate:laterDate]) {
NSLog(@"current-%@比later-%@晚",currentDate,laterDate);
}
// 比较哪个时间早
if ([currentDate earlierDate:earlierDate]) {
NSLog(@"current-%@ 比 earlier-%@ 早",currentDate,earlierDate);
}
if ([currentDate compare:earlierDate]==NSOrderedDescending) {
NSLog(@"current 晚");
}
if ([currentDate compare:currentDate]==NSOrderedSame) {
NSLog(@"时间相等");
}
if ([currentDate compare:laterDate]==NSOrderedAscending) {
NSLog(@"current 早");
}
总算在百忙之中把NS常用类的基础知识整理完了,如果有什么写不对,写不全的地方,欢迎大家来留言反馈。作为知识共享,首先要自己能明白,自己能有自己的想法,然后再给大家展现自己的想法,希望能抛砖引玉~之后会开始整理UI部分以及各种小知识点~敬请期待