初步swift语言学习笔记8(保留了很多OC实现)
虽然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语言学习笔记8(保留了很多OC实现)的更多相关文章
- 初步swift语言学习笔记9(OC与Swift杂)
笔者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/34440159 转载请注明出处 假设认为文章对你有所帮助,请通过留言 ...
- 初步swift语言学习笔记2(可选类型?和隐式可选类型!)
作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/28904115 转载请注明出处 假设认为文章对你有所帮助.请通过留言 ...
- 初步swift语言学习笔记6(ARC-自己主动引用计数,内存管理)
笔者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/31824179 转载请注明出处 假设认为文章对你有所帮助.请通过留言 ...
- 【Swift】从零开始的Swift语言学习笔记-1:前言&Hello World
该系列分为两个大的部分. Swift基本语法 使用Xcode编写iOS应用程式 两个部分会双线并行更新. 本人的学习资料大多为Apple Develop官方上的生肉,难免会有疏漏,望斧正. 另外该系列 ...
- 【SWIFT】从零开始的SWIFT语言学习笔记-2:简单值、数组与字典
1.0.3 简单值.数组与字典 知识点: 使用var创建变量 var myVariable = 65 myVariable = myVariable + 1 使用let创建常量 let myConst ...
- 一份关于Swift语言学习资源的整理文件
一份关于Swift语言学习资源的整理文件 周银辉 在这里下载 https://github.com/ipader/SwiftGuide
- HTML语言学习笔记(会更新)
# HTML语言学习笔记(会更新) 一个html文件是由一系列的元素和标签组成的. 标签: 1.<html></html> 表示该文件为超文本标记语言(HTML)编写的.成对出 ...
- 2017-04-21周C语言学习笔记
C语言学习笔记:... --------------------------------- C语言学习笔记:学习程度的高低取决于.自学能力的高低.有的时候生活就是这样的.聪明的人有时候需要.用笨的方法 ...
- 2017-05-4-C语言学习笔记
C语言学习笔记... ------------------------------------ Hello C语言:什么是程序:程序是指:完成某件事的既定方式和过程.计算机中的程序是指:为了让计算机执 ...
随机推荐
- POJ1201 差分约束
给定ai,bi, ci 表示区间[ai,bi]内至少有ci个点, 要求对于所有给定的ai,bi,ci, 至少多少个点才能满足题目的条件 重做这一题学到的一点是, 可以设变量来表示一些东西,然后才能找 ...
- 在Amazon AWS RHEL 7上安装 配置PPTP VPN
0 前言 0.1 为什么需要VPN? 国内的VPN不是必须,但是国外的VPN是很有用的.连接到国外的VPN服务器之后就可以访问Google,Facebook, Youtube等网站,没有Google的 ...
- Intel baytrail-t support Linux?
点击这里查看文章 有空试试---唉... Ubuntu (Linux) on Atom Z3700 Series ASUS Transformer Book T100 is particularly ...
- bootstrap在 刷新页面,tab选择页面不会改变。
您可以直接复制代码 注意在同级别文件夹中引用 相应js 和 css. 实现tab影响 关键看bootstrap的 data-toggle= tab <html lang="en&quo ...
- SplitContainer如何实现左侧导航,正确显示和导航内容
关于这种类型的设计有很多的实现,这样,我首先解释一下我使用: 我的原则是实现方式,将form嵌panel在,作为一个子窗口. 如下面的代码细节: Form form = new DataSelect( ...
- swift-辞典NSDictionary定义,变化的关键,删/加入关键
// Playground - noun: a place where people can play import UIKit //--------------------------------- ...
- hdu 2074 堆放篮 好开心图纸标题
堆放篮 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- nodeValue的兼容问题
nodeValue获取Text或Comment元素的文本值. 在IE6.IE7.IE8中游览器会自作聪明的去掉前面的空白字符text,而其它现代游览器则会保留空白 <body> <s ...
- MVC Razor标签
1. RenderBody在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面中,会看到标签里有这样一条语句:@Rend ...
- iframe页面控制父页面跳转
<script> window.onload=function(){ window.location.href="http://www.baidu.com"; ...