惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 房客

用JavaDoc生成项目文档 thymeleaf参考手册 转的一个Java基本功 杂记 修改Esxi克隆的CentOS的IP地址 CentOS搭建socket5代理服务器 CentOS上搭建Nginx + Mono 运行 asp.net 启动PPT的时候一直配置vs2013的问题解决 swift 元组 swift 集合类型(二) swift 集合类型(一) swift UIImage加载远程图片和圆角矩形 swift 随机生成背景颜色 VSFTPD配置TLS/SSL JayProxy的设置 CentOS 安装配置 PPTP VPN 服务器 - 房客 Nginx + fastcgi 处理php 编译安装php5.3.8(含php-fpm) CentOS 安装eaccelerator PHP加速
swift 定时器的使用
房客 · 2014-06-17 · via 博客园 - 房客

在swift中,要使用定时器就需要用到对象NSTimer。通过NSTimer的实例化后,就可以调用fire方法来启用了。

NSTimer有2个构造函数

init(timeInterval ti: NSTimeInterval, invocation: NSInvocation!, repeats yesOrNo: Bool) -> NSTimer

init(timeInterval ti: NSTimeInterval, target aTarget: AnyObject!, selector aSelector: Selector, userInfo: AnyObject!, repeats yesOrNo: Bool) -> NSTimer

通过我的实践后,发现这2个构造函数都是假的,基本上不能使用。第一个构造函数的invocation参数貌似swift还没有实现(xCode6 beta)通过mac+left click点进去后,里面啥也没有。到是在stackoverflow上看到有大神使用的,没有尝试:

NSMethodSignature *sgn = [self methodSignatureForSelector:@selector(onTick:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sgn];
[inv setTarget: self];
[inv setSelector:@selector(onTick:)];

NSTimer *t = [NSTimer timerWithTimeInterval: 1.0
                      invocation:inv 
                      repeats:YES];

显然在swift中,还没有NSInvocation.invocationWithMethodSignature方法的实现。

第二个构造函数,通过它可以实例化NSTimer对象,但只能触发一次。之后就没用了,貌似repeats的参数是没有作用的。

var timer = NSTimer(1.0, target: self, selector: "timerFireMethod:", userInfo: nil, repeats:true);
timer.fire()

后来看到网上大家都使用的静态函数的形式实例化,也尝试了一下,竟然成功了:

    func doTimer(){
        var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "timerFireMethod:", userInfo: nil, repeats:true);
        timer.fire()
    }
    func timerFireMethod(timer: NSTimer) {
        var formatter = NSDateFormatter();
        formatter.dateFormat = "MM/dd/yyyy HH:mm:ss"
        var strNow = formatter.stringFromDate(NSDate())
        txta.text  = "\(strNow)"
    }

程序的目的就是在view上的label中实时显示当前的时间。

总结:

1、NSTimer只能使用静态函数来实例化该对象

2、使用静态函数来实例化对象的时候,竟然需要传一个实例对象的方法(通过selector),这个感觉有些违背面向对象的思想的。任何对象的成员的访问都需要通过实例化对象来调用。swift中有全局函数,想想也释然了,毕竟他是一门刚出来的语言还是需要时间沉淀的。

3、在模拟器中,运行10分钟,程序所使用的memory,从12.0MB上升到了12.5MB。感觉对内存的要求还是比较高的,不知道有没有其它更好的方法实现。