NSValue:
+ (NSValue *)valueWithPointer:(constvoid *)pointer;
NSNumber :NSValue(的子类)
@property (readonly,copy)NSString *stringValue; // 字符串
@property (readonly)char charValue; // 值
@property (readonly)unsignedchar unsignedCharValue;
@property (readonly)short shortValue;
- (NSNumber *)initWithChar:(char)value; // 初始化
- (NSNumber *)initWithUnsignedChar:(unsignedchar)value;
- (NSNumber *)initWithShort:(short)value;
+ (NSNumber *)numberWithChar:(char)value; // 便利构造器
+ (NSNumber *)numberWithUnsignedChar:(unsignedchar)value;
+ (NSNumber *)numberWithShort:(short)value;
...
NSSet:
@property (readonly)NSUInteger count;
@property (readonly,copy)NSArray *allObjects;
- (id)anyObject; // 某个
- (NSSet *)setByAddingObject:(id)anObject; // 添加
+ (instancetype)set; // 类方法赋值
+ (instancetype)setWithObject:(id)object;
+ (instancetype)setWithObjects:(id)firstObj, ...;
+ (instancetype)setWithSet:(NSSet *)set;
+ (instancetype)setWithArray:(NSArray *)array;
- (instancetype)initWithObjects:(id)firstObj, ...; // 初始化
- (instancetype)initWithSet:(NSSet *)set;
- (instancetype)initWithArray:(NSArray *)array;
NSData:
@property (readonly)NSUInteger length; // 长度
- ( instancetype )initWithData:( NSData *)data;
+ (instancetype)data; // 便利构造器
+ (instancetype)dataWithContentsOfFile:(NSString *)path; // 来自文件
+ (instancetype)dataWithData:(NSData *)data;
NSDate:
@property (readonly)NSTimeInterval timeIntervalSinceNow; // 距今
@property (readonly)NSTimeInterval timeIntervalSince1970; // 从1970(计算机大部分都是从1970开始计时)
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs; // 初始化
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
+ (instancetype)date; // 便利构造器 // 现在时间
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; // 相距多少秒时间
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
时间的格式
yyyy-MM-dd HH-mm-ss
y年 M月 d天 H24小时制,h12小时制 m分 s秒
设置一下时间的格式,要转换的时间要和格式相吻合
通过这种方式,系统还会切换成当前的时间
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
NSDate *date = [NSDate date];
NSString *strDate = [formatter stringFromDate:date];
NSLog(@"%@", strDate);
字符串 -> NSDate
把时间又减掉八小时
NSString *timeStr = @"2015-7-23 17-18-10";
NSDate *timeDate= [formatter dateFromString:timeStr];
NSLog(@"%@", timeDate);
关于日期的一些代码
NSDate *date = [NSDate date];
NSLog(@"%@", date);
// +date获取的时间无论在哪个区,都是打印相对应的零时区的时间
// 获取当前所在的时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSLog(@"%@", zone);
// 获取一个和0时区相差的秒数
NSInteger seconds = [zone secondsFromGMTForDate:date];
NSLog(@"%ld", seconds);
// 通过相差秒数,能获取到现在的时间
NSDate *localDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
NSLog(@"%@", localDate);//
NSDate *times = [NSDate dateWithTimeIntervalSince1970:0];
NSLog(@"%@", times);
// 一个明天这个时候的时间
NSDate *tomorrowDate = [NSDate dateWithTimeIntervalSinceNow:(32 * 3600)];
NSLog(@"%@", tomorrowDate);
// 昨天这个时候的时间
NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:(- 16 * 3600)];
NSLog(@"%@", yesterdayDate);
// 时间间隔(double)
// 计算两个对象的时间间隔
NSDate *stableDate = [NSDate dateWithTimeIntervalSinceNow:(arc4random() % (3600 * 24 + 1))];
NSLog(@"%@", stableDate);
NSTimeInterval interval =[stableDate timeIntervalSinceDate:date];
NSLog(@"%g", interval);
if (interval <= 60) {
NSLog(@"刚刚");
} else if (interval >= 60 && interval < 3600) {
NSLog(@"%ld分钟前", (NSInteger)interval/60);
} else if (interval >= 3600 && interval < 24 * 3600) {
NSLog(@"%ld小时前", (NSInteger)interval/3600);
}
拼接时间段字符串
.h
#import <Foundation/Foundation.h>
@interface Events : NSObject
@property(nonatomic, copy)NSString *begin_time;
@property(nonatomic, copy)NSString *end_time;
- (id)initWithBegin_time:(NSString *)begin_time
andEnd_time:(NSString *)end_time;
+ (id)eventsWithBegin_time:(NSString *)begin_time
andEnd_time:(NSString *)end_time;
- (NSString *)newtime;
@end
.m
<pre name="code" class="objc">#import "Events.h"
@implementation Events
- (id)initWithBegin_time:(NSString *)begin_time
andEnd_time:(NSString *)end_time{
self = [super init];
if (self) {
_begin_time = begin_time;
_end_time = end_time;
}
return self;
}
+ (id)eventsWithBegin_time:(NSString *)begin_time
andEnd_time:(NSString *)end_time{
Events *events = [[Events alloc]initWithBegin_time:begin_time andEnd_time:end_time];
return events;
}
- (NSString *)newtime{
NSRange r =NSMakeRange(2, 2);
NSString *begin = [self.begin_time substringWithRange:r];
NSString *end = [self.end_time substringWithRange:r];
while ([begin isEqualTo: end]) {
r.location += 3;
if (r.location == 11) {
break;
}
begin = [self.begin_time substringWithRange:r];
end = [self.end_time substringWithRange:r];
}
if (r.location == 2) {r.location = 0;}
NSString *newTime = [NSString stringWithFormat:@"%@ - %@", self.begin_time, [self.end_time substringFromIndex:r.location]];
return newTime;
}
@end
main
#import <Foundation/Foundation.h>
#import "Events.h"
int main(int argc, const char * argv[]) {
NSString *filePath = @"/Users/dllo/Desktop/z+7/OC07_NSDate/OC07_NSDate/activitylist.txt";
NSData *data = [NSData dataWithContentsOfFile:filePath options:0 error:nil];
NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
for (NSDictionary *eventDic in dic[@"events"]) {
NSString *begin_time = eventDic[@"begin_time"];
NSString *end_time = eventDic[@"end_time"];
Events *events = [Events eventsWithBegin_time:begin_time andEnd_time:end_time];
NSString *newTime = [events newtime];
NSLog(@"%@", newTime);
}
return 0;
}