温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Foundation框架(7)时间操作

发布时间:2020-07-11 06:49:22 来源:网络 阅读:492 作者:ymanmeng123 栏目:移动开发

时间操作相关的主要类有:

    NSDate

    NSDatFormatter

    NSCalendar

    NSDateComponents


另外介绍一个定时器对象NSTimer


  • NSDate

用于描述一个日期时间,提供了一些基本的时间获取和比较方法

+ (instancetype)date  //当前日期时间
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)seconds 
- (NSComparisonResult)compare:(NSDate *)anotherDate
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate


  • NSDateFormatter

用于字符串格式化NSDate对象     

@property(copy) NSString *dateFormat  //格式
- (NSString *)stringFromDate:(NSDate *)date
- (NSDate *)dateFromString:(NSString *)string


  • NSCalendar

日历对象,在日历对象之上,进行NSDate与NSDateComponents的转换

+ (NSCalendar *)currentCalendar
- (NSDateComponents *)components:(NSCalendarUnit)unitFlags fromDate:(NSDate*)date
- (NSDate *)dateFromComponents:(NSDateComponents *)comps


  • NSDateComponents

用于对象的方式描述NSDate对象

@property NSInteger year
@property NSInteger month
@property NSInteger day
@property NSInteger hour
@property NSInteger minute
@property NSInteger second
...


  • NSTimer

NSTimer用于启动一个定时,定时执行指定的操作

指定的操作用SEL(方法)对象指定

//直接启动定时器
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats
//创建一个不直接启动的定时器
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)seconds target:(id)target selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)repeats

定时器的其他控制方法:

- (void)fire    //启动
- (void)invalidate //使失效



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI