初步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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • kafka的主要作用_通俗的讲kafka是干什么的

    kafka的主要作用_通俗的讲kafka是干什么的作者:宝哥-NO1blog.csdn.net/code52/article/details/504755111、kafka是什么?Kafka是由LinkedIn开发的一个分布式基于发布/订…

    2022年10月13日
    3
  • fcn全卷积神经网络搭建_区域卷积神经网络

    fcn全卷积神经网络搭建_区域卷积神经网络文章目录简介核心思想网络结构上采样Upsampling跳级结构CNN与FCN的区别与联系网络训练过程存在问题【参考】简介全卷积网络(FullyConvolutionalNetworks,FCN)是JonathanLong等人于2015年在FullyConvolutionalNetworksforSemanticSegmentation一文中提出的用于图像语义分割的一种框架,是深度学习用于语义分割领域的开山之作。FCN将传统CNN后面的全连接层换成了卷积层,这样网络的输出将是热力图而非类

    2022年9月25日
    2
  • SMOTE算法及其python实现[通俗易懂]

    SMOTE算法及其python实现[通俗易懂]SMOTE(SyntheticMinorityOversamplingTechnique),合成少数类过采样技术.它是基于随机过采样算法的一种改进方案,由于随机过采样采取简单复制样本的策略来增加少数类样本,这样容易产生模型过拟合的问题,即使得模型学习到的信息过于特别(Specific)而不够泛化(General),SMOTE算法的基本思想是对少数类样本进行分析并根据少数类样本人工合成新样本添加

    2022年6月16日
    66
  • 2020 4.13 idea激活码【在线破解激活】

    2020 4.13 idea激活码【在线破解激活】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月15日
    52
  • uboot启动过程详解

    uboot启动过程详解

    2022年3月2日
    33
  • TranslateMessage函数 (转)「建议收藏」

    TranslateMessage函数 (转)「建议收藏」TranslateMessage是用来把虚拟键消息转换为字符消息。由于Windows对所有键盘编码都是采用虚拟键的定义,这样当按键按下时,并不得字符消息,需要键盘映射转换为字符的消息。TranslateMessage函数用于将虚…

    2025年11月6日
    4

发表回复

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

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