
初始化器
一个类可以有多个初始化器,但通常只有一个指派初始化器,其他的初始化器调用指派初始化器来进行初始化。
创建初始化器的规则:
1:假如父类的初始化器够用,就没有必要在自定义类中再创建初始化器。//这个是显然的事情是不是。
2:假如已经决定要创建一个初始化器,就必须重载父类的指派初始化器。//这个是应该的,要不然不小心调用了父类的指派初始化器怎么办?
3:假如创建了多个初始化器,让其中一个来做初始化工作,那就是指派初始化器,其他的初始化器来调用它就可以了。
4:子类需要调用父类的指派初始化器。
读者可能会创建一个需要提供参数的类。这个时候,可以重载父类的指派初始化器来抛出异常:
- (ID) init
{
@throw [NSExceptionexceptionWithname:@"异常名称" reason:@"异常原因" userInfo:[NSDictionarydictionaryWithObject:@"附带信息" forKey:@"key"]];
return nil;
}
NSDate
1:[NSDatedate]得到的时间是格林威治时间。
2:- (ID) dateByAddingTimeInterval : (NSTimeInterval) ti NS_AVAILABLE(10_6,2_0);返回一个增加ti秒数后的时间。
3:- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;返回两个时间的秒数差
如:NSTimeInterval inter = [date1timeIntervalSinceDate:date2];如果date1比date2早则返回一个负值。
例如如下代码:
//2014-11-02 22:11:49.449 YYY[796:303] 2014-11-02 14:11:49 +0000
NSDate *date1 = [NSDatedate];NSLog(@"%@",date1);
//看上面的代码段你会发现,输出的并不是当前北京时间。
//2014-11-02 22:11:49.449 YYY[796:303] 2014-11-02 14:28:29 +0000
date1 = [date1 dateByAddingTimeInterval:1000];NSLog(@"%@",date1);
NSDate *date2 = [NSDatedate];NSTimeInterval inter = [date2 timeIntervalSinceDate:date1];
//也许你会以为输出的是-1000,其实基本上不会,特别是打断点的情况下,那就取决于date1 = [date1 dateByAddingTimeInterval:1000];这段代码执行时间与NSDate *date2 = [NSDate date];这段代码执行时间之间的差距了,即使没有断点,输出也不是-1000,例如: -999.999462等。
NSLog(@"%f",inter);
4:- (NSTimeInterval) timeIntervalSinceReferenceDate; 返回GMT时间2001年1月1日与对象时间之间的秒差。
//self.myDate是在另一个地方初始化的
NSTimeInterval inter2 = [self.myDatetimeIntervalSinceReferenceDate];
//返回GMT时间2001年1月1日与对象时间之间的秒差。
NSLog(@"%f",inter2);
5:- (NSComparisonResult)compare:(NSDate *)other;比较两个时间点。如果两个参数中有一个为空,结果都是0。正常情况是结果为0表示相等的意思,如果结果为-1则表示升序的意思。
NSComparisonResult compareresult = [self.myDatecompare:date1];
NSLog(@"%li",compareresult);
/*
//当self.myDate为空的时候,输出的是0。
2014-11-02 22:29:56.749 YYY[999:303] (null),2014-11-02 14:46:36 +0000
2014-11-02 22:29:58.874 YYY[999:303] 0
*/
NSComparisonResult compareresult2 = [date1compare:self.myDate];
NSLog(@"%li",compareresult2);
/*
2014-11-02 22:32:59.999 YYY[1060:303] 0//当self.myDate为空的时候,输出的是0。
*/
NSTimeZone
//管理时区的类。
NSTimeZone *zone = [NSTimeZonesystemTimeZone];NSLog(@"%@",zone);
NSDate *date = [NSDatedate];NSLog(@"%@",date);
//返回当前时区与格林威治时间之间的时间差(使用秒数表示)。
NSInteger intint = [zonesecondsFromGMT];NSLog(@"%li",(long)intint);
//返回所有的时区名,可以用这些时区明生成时区。
NSArray *arry = [NSTimeZonekNownTimeZonenames];NSLog(@"%@",arry);
//使用时区名来生成一个NSTimeZone对象
NSTimeZone *zone2 = [NSTimeZonetimeZoneWithname:@"Europe/athens"];
NSInteger int2 = [zone2secondsFromGMT];NSLog(@"%ld",(long)int2);//@"Pacific/Ponape",39600
NSTimeZone *defaultTimeZone = [NSTimeZonedefaultTimeZone];
NSLog(@"%@",defaultTimeZone);//Asia/Harbin (GMT+8) offset 28800
NSTimeZone *localTimeZone = [NSTimeZonelocalTimeZone];
//Local Time Zone (Asia/Harbin (GMT+8) offset 28800)
NSInteger secondsFromGMT = [localTimeZonesecondsFromGMT];
//根据时区和NSDate获取当前本地时间
NSDate *localDate = [NSDatedate];
localDate = [localDate dateByAddingTimeInterval:secondsFromGMT];
NSLog(@"%@",localDate);
NSCalendar与NSDateComponents
//如果你想获得某个时间以后或者以前相隔指定时间段的时间可以使用下面的方法。
NSCalendar *currentCalendar = [NSCalendarcurrentCalendar];
NSDateComponents *weekComponents = [[NSDateComponentsalloc]init];
[weekComponents setYear:1];
NSLog(@"%ld",NSNotFound);
NSLog(@"%ld",weekComponents.era);//压根就是NSNotFound
NSLog(@"%ld",weekComponents.year);//输出1.
NSDate *Now = [NSDatedate];
NSDate *date = [currentCalendardateByAddingComponents:weekComponentstoDate:Now options:0];
NSDateFormatter *format = [[NSDateFormatteralloc]init];
[format setTimeStyle:NSDateFormatterShortStyle];
[format setDateStyle:NSDateFormatterFullStyle];
NSLog(@"%@",[formatstringFromDate:date]);
NSDateFormatter
- (voID)setDateFormat:(Nsstring *)string;
下面绿色部分来源于网络
关于DateFormat的格式,先给出几个例子如下:
NSDate *date = [NSDate date];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd/MM/yyyy hh:mm:ss aaa"];
NSLog(@"curr=%@,dateFormate=%@",[df stringFromDate:date],df.dateFormat);
变化dataFormat就可以变化时间的输出格式
curr=27/04/2013 03:42:21 PM,dateFormate=dd/MM/yyyy hh:mm:ss aaa
curr=02013-4月-27 公元 03:45 PM,dateFormate=yyyyy-MMMM-dd GGG hh:mm aaa
curr=3:45 PM,格林尼治标准时间+0800,dateFormate=K:mm a,z
curr=03 o'clock PM,中国标准时间,dateFormate=hh 'o''clock' a,zzzz
curr=3:46 PM,dateFormate=h:mm a
curr=周六,4月 27,'13,dateFormate=EEE,MMM d,''yy
curr=2013:04:27 公元 at 15:47:11 格林尼治标准时间+0800,dateFormate=yyyy:MM:dd G 'at' HH:mm:ss zzz
这是在网上找的几个例子,如果要显示成自己希望的格式,那么还需要自己写出格式来
消息机制工作原理
NSLog(@"%ld",@selector(last));
NSLog(@"%ld",@selector(exception));
NSLog(@"%ld",@selector(currentCalendar));
NSLog(@"%ld",@selector(timezone));
/*
2014-11-03 16:01:09.387 YYY[17055:303] 140735681813445
2014-11-03 16:01:09.969 YYY[17055:303] 140735710391751
2014-11-03 16:01:10.519 YYY[17055:303] 140735568670500
2014-11-03 16:01:11.044 YYY[17055:303] 4294976821
*/
//objc_msgSend(self,4294976821,nil);//真的跳转到timezone函数里面去了。
总结
以上是内存溢出为你收集整理的2014-11-03号 Cocoa编程 原书第4版 学习笔记 002全部内容,希望文章能够帮你解决2014-11-03号 Cocoa编程 原书第4版 学习笔记 002所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)