初步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语言:什么是程序:程序是指:完成某件事的既定方式和过程.计算机中的程序是指:为了让计算机执 ...
随机推荐
- ecshop 调用其他数据库中的商品
ecshop中修改includes/cls_ecshop.php中第53行 function table($str) { /* if($str=='goods'){ return '`ecshop3' ...
- WPF换肤之三:WPF中的WndProc
原文:WPF换肤之三:WPF中的WndProc 在上篇文章中,我有提到过WndProc中可以处理所有经过窗体的事件,但是没有具体的来说怎么可以处理的. 其实,在WPF中,要想利用WndProc来处理所 ...
- Windows Phone开发(38):动画之PointAnimation
原文:Windows Phone开发(38):动画之PointAnimation PointAnimation也是很简单的,与前面说到的两个Animation是差不多的,属性也是一样的,如By.Fro ...
- python学习笔记之七:魔法方法,属性
在python中,有的名称会在前面和后面加上两个下划线,由这些名字组成的集合所包含的方法称为魔法方法(或者是特殊方法).如果对象实现了这些方法中的某一个,那么这个方法会在特殊的情况下(确切地说是根据名 ...
- Android 解决Gallery下ScrollView滑动事件冲突
在Gallery下,里面内容过长超出屏幕,这时我们可以用ScrollView来滚动,但是这样做了以后,会发现一个问题,Gallery的滑动事件和ScrollView的滑动事件起冲突,这时我们可以自定义 ...
- hihocoder第42周 3*N骨牌覆盖(状态dp+矩阵快速幂)
http://hihocoder.com/contest/hiho42/problem/1 给定一个n,问我们3*n的矩阵有多少种覆盖的方法 第41周做的骨牌覆盖是2*n的,状态转移方程是dp[i] ...
- 第三方框架和ARC
在使用了ARC机制的项目中使用第三方开源框架的方法: 1.在第三方开源框架的每个.m文件都设置成 -fno-objc-arc 具体方法:TARGETS--->Build Phases -- ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- 【原创】leetCodeOj ---Construct Binary Tree from Preorder and Inorder Traversal 解题报告
原题地址: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题目 ...
- js 加载初始化日期
//初始化加载时间 $(function(){ var now = new Date(); var year = now.getFullYear(); //年 ...