swift语言的学习笔记八(保留了许多OC的实现)

来源:互联网 发布:java traceid 追踪 编辑:程序博客网 时间:2024/06/10 10:16

尽管swift作为一门新语言,但还保留了许多OC的机制,使得swift和OC更好的融合在一起。如果没有OC基础的先GOOGLE一下。

如:KVO,DELEGATE,NOTIFICATION。

详见DEMO。

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import Foundation  
  2.   
  3. @objc   // 需要打开objc标识,否则@optional编译出错  
  4.   
  5. protocol kvoDemoDelegate {  
  6. func willDoSomething()  
  7. @optional  func didDoSomething()  //可选实现,  
  8. }  
  9.   
  10. let ntfname = "test_notification"  
  11.   
  12. class kvoDemo : NSObject //不写NSObject默认就是从NSObject来的  
  13. {  
  14.     var delegate: kvoDemoDelegate!  
  15.       
  16.     var presult : Double = 0.0  
  17.       
  18.     var result : Double {  
  19.         get{  
  20.             return presult;  
  21.         }  
  22.       
  23.         set{  
  24.           self.presult = newValue  
  25.         }  
  26.     }  
  27.       
  28.     init()  
  29.     {  
  30.           
  31.     }  
  32.       
  33.     func doSomething()  
  34.     {  
  35.         if let yet = self.delegate?  
  36.         {  
  37.             delegate!.willDoSomething()  
  38.         }  
  39.           
  40.         for _ in 1..5  
  41.         {  
  42.             println("i'm doing now,don't touch me,please.")  
  43.         }  
  44.           
  45.         if let yet = self.delegate?  
  46.         {  
  47.             delegate!.didDoSomething!()  
  48.         }  
  49.     }  
  50.       
  51.     func notificationPost()  
  52.     {  
  53.         let ntf = NSNotificationCenter.defaultCenter()  
  54.         ntf.postNotificationName(ntfname, object :nil, userInfo:nil)  
  55.     }  
  56.       
  57.     deinit  
  58.     {  
  59.           
  60.     }  
  61. }  
测试:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class ViewController: UIViewController,kvoDemoDelegate {  
  2.       
  3.     //KVO  
  4.     override func observeValueForKeyPath(keyPath: String?, ofObject: AnyObject?, change: NSDictionary?, context: CMutableVoidPointer)  
  5.     {  
  6.         if keyPath == "result"  
  7.         {  
  8.             var newvalue : AnyObject? = change?.objectForKey("new");  
  9.             println("the new value is \(newvalue)")  
  10.         }  
  11.     }  
  12.       
  13.     //delegate  
  14.     func willDoSomething()  
  15.     {  
  16.         println("i will do it.")  
  17.     }  
  18.       
  19.     func didDoSomething()  
  20.     {  
  21.         println("i had do it.")  
  22.     }  
  23.       
  24.     //notification  
  25.     func onRecviceNotification(notification:NSNotification)  
  26.     {  
  27.         println("Recevice notification \(notification)")  
  28.     }  
  29.   
  30.     override func viewDidLoad() {  
  31.         super.viewDidLoad()  
  32.         // Do any additional setup after loading the view, typically from a nib.  
  33.           
  34.         var kvo = kvoDemo()  
  35.         kvo.addObserver(self, forKeyPath: "result", options: NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Old, context: nil)  
  36.   
  37.         kvo.result = 280.0  
  38.           
  39.         kvo.removeObserver(self,forKeyPath:"result",context: nil)  
  40.           
  41.         kvo.delegate = self  
  42.         kvo.doSomething()  
  43.           
  44.         let ntf = NSNotificationCenter.defaultCenter()  
  45.         ntf.addObserver(self, selector:"onRecviceNotification:", name :ntfname, object : nil)  
  46.         kvo.notificationPost()  
  47.         ntf.removeObserver(self)  
  48.     }  
  49. }  

结果:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. the new value is 280  
  2. i will do it.  
  3. i'm doing now,don't touch me,please.  
  4. i'm doing now,don't touch me,please.  
  5. i'm doing now,don't touch me,please.  
  6. i'm doing now,don't touch me,please.  
  7. i had do it.  
  8. Recevice notification NSConcreteNotification 0x10be60930 {name = test_notification}  

0 1
原创粉丝点击