swift 定时器的使用


参考: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, invocation: NSInvocation, repeats yesOrNo: Bool) -> NSTimer
    class func scheduledTimerWithTimeInterval(ti: NSTimeInterval, target aTarget: AnyObject, selector aSelector: Selector, userInfo: AnyObject?, 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)")
        
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值