iOS 备忘录
iOS 时区:
1 //先是将指定时区的日期转换为当前时区时间: 2 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 3 [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; 4 [formatter setTimeZone:[NSTimeZone defaultTimeZone] ] ; 5 NSDate *time = [formatter dateFromString:@"2011-09-27 10:11:30 +0600"]; 6 NSLog(@"Local time is %@",[formatter stringFromDate: time]);
1 //然后是将当前时区时间转换为特定时区的: 2 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 3 [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; 4 [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Tokyo"]]; 5 NSLog(@"Now Tokyo time is %@",[formatter stringFromDate:[NSDate date]]);
1 //下面是将指定时区的某个日期时间转化为指定时区的时间 2 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 3 [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"]; 4 [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Tokyo"]]; 5 NSDate *time = [formatter dateFromString:@"2011-09-27 10:11:30 +0600"]; 6 NSLog(@"Tokyo time is %@",[formatter stringFromDate: time]);
1 //NSDate转NSString: 2 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 3 [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 4 NSString *strDate = [dateFormatter stringFromDate:[NSDate date]]; 5 NSLog(@"%@", strDate);
iOS 语法糖
NSNumber 可以用@数字来构造,如@3或者@(3); NSArray 可以用@[]构造,如@[v1,v2,v3]; NSDictionary可以用@{}构造,如@{k1:v1,k2:v2};
iOS版本号获取
NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];