swift 定时器的使用
2015-08-19 10:30
393 查看
参考:http://www.cnblogs.com/sxlfybb/p/3792611.html
NSTimer有2个构造函数
NSTimer有2个静态实例
在swift中,找不到 NSInvocation 类,日了狗了
还好有另一种使用静态函数的形式去实例化:
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)") }
相关文章推荐
- 使用Swift创建一个SOAP的请求
- Swift详解之一 -------- 变量声明
- Swift-函数
- Swift-字典
- Swift-数组
- Swift-字符串
- Swift-循环
- Swift-分支
- 慕课玩儿转swift笔记02-字符串
- IOS autolayout自动布局实例(swift)
- 《转》OpenStack对象存储——Swift
- Swift初学有一点难理解的东西,整理了一下,想明白了。
- iOS开发系列--Swift语言
- swift学习1
- SWIFT缩写
- 《The Swift Programming Language》原版中的问题汇总
- Swift-Optional(可选类型?!)
- Swift-初学
- iOS 自定义UITabbarController中的item (swift)
- 最近正在参与的Swift2.0翻译工作(Access Control章节)