日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2024/9/30 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 初探swift语言的学习笔记八(保留了许多OC的实现) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/32715833 轉(zhuǎn)載請注明出處 如果覺得文章對你有所幫助,請通過留言或關(guān)注微信公眾帳號fengsh998來支持我,謝謝!


盡管swift作為一門新語言,但還保留了許多OC的機(jī)制,使得swift和OC更好的融合在一起。如果沒有OC基礎(chǔ)的先GOOGLE一下。

如:KVO,DELEGATE,NOTIFICATION。

詳見DEMO。

[cpp]?view plaincopy
  • import?Foundation??
  • ??
  • @objc???//?需要打開objc標(biāo)識,否則@optional編譯出錯??
  • ??
  • protocol?kvoDemoDelegate?{??
  • func?willDoSomething()??
  • @optional??func?didDoSomething()??//可選實現(xiàn),??
  • }??
  • ??
  • let?ntfname?=?"test_notification"??
  • ??
  • class?kvoDemo?:?NSObject?//不寫NSObject默認(rèn)就是從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??
  • ????{??
  • ??????????
  • ????}??
  • }??
  • 測試:

    [cpp]?view plaincopy
  • 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)??
  • ????}??
  • }??

  • 結(jié)果:

    [cpp]?view plaincopy
  • 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}??
  • 總結(jié)

    以上是生活随笔為你收集整理的初探swift语言的学习笔记八(保留了许多OC的实现)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。