初步swift语言学习笔记8(保留了很多OC实现)
2015-09-02 08:02
525 查看
笔者:fengsh998原文地址:http://blog.csdn.net/fengsh998/article/details/32715833转载请注明出处假设认为文章对你有所帮助,请通过留言或关注微信公众帐号fengsh998来支持我,谢谢!
虽然swift作为一门新语言,但还保留了很多OC的机制,使得swift和OC更好的融合在一起。假设没有OC基础的先GOOGLE一下。
如:KVO。DELEGATE。NOTIFICATION。
详见DEMO。
结果:
虽然swift作为一门新语言,但还保留了很多OC的机制,使得swift和OC更好的融合在一起。假设没有OC基础的先GOOGLE一下。
如:KVO。DELEGATE。NOTIFICATION。
详见DEMO。
import Foundation @objc // 须要打开objc标识,否则@optional编译出错 protocol kvoDemoDelegate { func willDoSomething() @optional func didDoSomething() //可选实现, } let ntfname = "test_notification" class kvoDemo : NSObject //不写NSObject默认就是从NSObject来的 { var delegate: kvoDemoDelegate! var presult : Double = 0.0 var result : Double { get{ return presult; } set{ self.presult = newValue } } init() { } func doSomething() { if let yet = self.delegate? { delegate!.willDoSomething() } for _ in 1..5 { println("i'm doing now,don't touch me,please.") } if let yet = self.delegate? { delegate!.didDoSomething!() } } func notificationPost() { let ntf = NSNotificationCenter.defaultCenter() ntf.postNotificationName(ntfname, object :nil, userInfo:nil) } deinit { } }測试:
class ViewController: UIViewController,kvoDemoDelegate { //KVO override func observeValueForKeyPath(keyPath: String?, ofObject: AnyObject?, change: NSDictionary?, context: CMutableVoidPointer) { if keyPath == "result" { var newvalue : AnyObject? = change? .objectForKey("new"); println("the new value is \(newvalue)") } } //delegate func willDoSomething() { println("i will do it.") } func didDoSomething() { println("i had do it.") } //notification func onRecviceNotification(notification:NSNotification) { println("Recevice notification \(notification)") } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. var kvo = kvoDemo() kvo.addObserver(self, forKeyPath: "result", options: NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Old, context: nil) kvo.result = 280.0 kvo.removeObserver(self,forKeyPath:"result",context: nil) kvo.delegate = self kvo.doSomething() let ntf = NSNotificationCenter.defaultCenter() ntf.addObserver(self, selector:"onRecviceNotification:", name :ntfname, object : nil) kvo.notificationPost() ntf.removeObserver(self) } }
结果:
the new value is 280 i will do it. i'm doing now,don't touch me,please. i'm doing now,don't touch me,please. i'm doing now,don't touch me,please. i'm doing now,don't touch me,please. i had do it. Recevice notification NSConcreteNotification 0x10be60930 {name = test_notification}
相关文章推荐
- Swift中给UIView添加Badge
- Don’t Put View Code Into Your View Controller别把View创建的代码放在VC中(swift)
- 在Swift中检查API的可用性
- swift - The command pattern
- Swift1
- 初步swift语言学习笔记2(可选类型?和隐式可选类型!)
- Swift Tutorial(swift 教程) 4:Your First App
- swift开发
- swift 总结3
- 女神画廊App (Swift1.2)
- swift总结2
- 最老程序员创业开发实训5---Swift语言极速入门
- 相亲神器记录Swift1.2
- swift总结1
- swift自学注意点
- swift 中#pragma mark 表示方法
- swift详解之二十--------在xcode中使用git管理源代码,将代码提交到github
- swift 添加按钮
- swift - The Chain Responsibility pattern
- swift详解之十九--------------UITableView的基本操作(下拉刷新,新增删除,分组,检索等)