加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift 定时器的使用

发布时间:2020-12-14 01:54:25 所属栏目:百科 来源:网络整理
导读:参考:http://www.cnblogs.com/sxlfybb/p/3792611.html NSTimer有2个构造函数 /*not inherited*/ init(timeInterval ti: NSTimeInterval,invocation: NSInvocation,repeats yesOrNo: Bool)/*not inherited*/ init(timeInterval ti: NSTimeInterval,target aT


参考:http://www.cnblogs.com/sxlfybb/p/3792611.html

NSTimer有2个构造函数

/*not inherited*/ init(timeInterval ti: NSTimeInterval,invocation: NSInvocation,repeats yesOrNo: Bool)
/*not inherited*/ init(timeInterval ti: NSTimeInterval,target aTarget: AnyObject,selector aSelector: Selector,userInfo: AnyObject?,repeats yesOrNo: Bool)
注意到,注释为未继承,所以 两个构造不能用

NSTimer有2个静态实例

    class func scheduledTimerWithTimeInterval(ti: NSTimeInterval,repeats yesOrNo: Bool) -> NSTimer
    class func scheduledTimerWithTimeInterval(ti: NSTimeInterval,repeats yesOrNo: Bool) -> NSTimer
    

在swift中,找不到 NSInvocation 类,日了狗了

还好有另一种使用静态函数的形式去实例化:

    func mNSTimer() {
        let timer = NSTimer.scheduledTimerWithTimeInterval(1.0,target: self,selector: "timerFireMethod:",userInfo: "generalbenil",repeats: true)
        timer.fire() // 启动定时器
    }
    // 参数: The timer passes itself as the argument
    func timerFireMethod(timer: NSTimer) {
        print("(timer.userInfo!)")
        let formatter = NSDateFormatter()
        formatter.dateFormat = "MM/dd/yyyy HH:mm:ss" // 这里注意时间的格式,大小写代表的意义不同!
        let strNow = formatter.stringFromDate(NSDate())
        print("the time is (strNow)")
        
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读