初步swift语言学习笔记8(保留了很多OC实现)

初步swift语言学习笔记8(保留了很多OC实现)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

笔者:fengsh998
原文地址:http://blog.csdn.net/fengsh998/article/details/32715833
转载请注明出处
假设认为文章对你有所帮助,请通过留言或关注微信公众帐号fengsh998来支持我,谢谢!

虽然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}

版权声明:本文博主原创文章,博客,未经同意不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117120.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • copy和deepcopy[通俗易懂]

    copy和deepcopy[通俗易懂]总结一下,如果一个集合中的元素是字符串,copy之后则是两个互不相干的新集合,内存地址也不一样,修改任意一个另一个不会做出改变,但是如果其中的元素有数组的话,那么情况就不一样了,copy后虽然集合是两个完全新的集合,但是集合中的数组指向的是同一个内存地址,在此集合中对其中的数组数据进行修改,另一个集合中的元素是数组的内容也会改变,如果想完完全全独立出来是两个独立的集合,就得使用deepcopy而不…

    2022年10月2日
    0
  • SVN服务器部署「建议收藏」

    SVN服务器部署「建议收藏」1,关闭防火墙或者开放对应端口3690,2,Yum下载安装subversion检查SVN是否安装成功svnserve–version3,建立SVN版本库3.1因为大磁盘挂载在/home下,所以我是在/home下创建的版本库,mkdir/home/svn创建一个文件夹(所有项目库都在此目录下创建)3.2创建svn版本库svnadmincreate/home/svn/liangz_artliangz_art才是项目版本库的名字,后面开发人员checkout时就要用这个名称,项

    2022年7月19日
    17
  • 20亿以内哪个数的约数个数最多,个数是多少?

    20亿以内哪个数的约数个数最多,个数是多少?

    2022年1月29日
    117
  • poi系列(二):通过poi、poi-ooxml读取写出excel

    poi系列(二):通过poi、poi-ooxml读取写出excel(根据网上多篇文档实践整理加工,非原创也非转载)本类主要是读取后缀为xlsx或xls的excel操作。需要导入包<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId>…

    2022年7月16日
    18
  • 360企业版 修改服务器ip,360企业版快速安装部署图文教程「建议收藏」

    360企业版 修改服务器ip,360企业版快速安装部署图文教程「建议收藏」360企业版V5.0.6.1310官方最新版类型:服务器区大小:177.0M语言:中文评分:4.5标签:立即下载内容索引安装须知安装环境推荐全新安装控制中心旧版本升级安装控制中心配置控制中心1.360企业版有旧版本(基于C/S架构,点此下载)和新版本(基于B/S架构,点此下载)。用户可以选择继续使用旧版本(终端无数量限制,但版本不再更新,也不再添加新的产品功能);2.已有用户进行新旧版本升级时,…

    2022年9月15日
    0
  • centos7重启apache、nginx、mysql、php-fpm命令

    centos7重启apache、nginx、mysql、php-fpm命令

    2021年10月27日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号