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


相关推荐

  • 定时任务执行原理_md5算法原理

    定时任务执行原理_md5算法原理前言最近在思考实现定时任务的几种方式,比如quartz,delayqueue,scheduleThreadPool,时间轮。在对比的同时,也了解了下其简单原理,在这里描述下我对时间轮算法实现定时任务的理解。时间轮定时使用方式@Testpublicvoidtest3()throwsInterruptedException{DateTimeFormatterformatter=DateTimeFormatter.ofPattern(“yyyy-MM-d

    2022年10月1日
  • PyCharm如何设置 “ctrl+滚轮” 实现字体的放大和缩小「建议收藏」

    PyCharm如何设置 “ctrl+滚轮” 实现字体的放大和缩小「建议收藏」一、PyCharm字体放大的设置File–>setting–>Keymap–>在搜索框中输入increase—>IncreaseFontSize(双击)–>在弹出的对话框中选择AddMouseShortcut–>弹出的对话框后按住ctrl键的同时鼠标滚轮向上滑。二、Pycharm字体缩小的设置:File–>setting–>Keymap–>在搜索框中输入decrease—>

    2022年8月27日
  • 利用java实现发送邮件

    利用java实现发送邮件电子邮件的应用非常广泛,常见的如在某网站注册了一个账户,自动发送一封激活邮件,通过邮件找回密码,自动批量发送活动信息等。很显然这些应用不可能和我们自己平时发邮件一样,先打开浏览器,登录邮箱,创建邮件再发送。本文将简单介绍如何通过Java代码来创建电子邮件,并连接邮件服务器发送邮件。电子邮件协议电子邮件在网络中传输和网页一样需要遵从特定的协议,常用的电子邮件协议包括SMTP,POP3,IMA…

    2022年5月15日
  • Offsetof用法「建议收藏」

    Offsetof用法「建议收藏」#include<stddef.h>#include<stdio.h>structaddress{charname[50];charstreet[50];intphone;};intmain(){printf(“address结构中的name偏移=%d字节。\n”,offsetof(structaddress,name));printf(“address结构中的street偏移=%d字节。\n”,offsetof(s

    2022年8月22日
  • 软件激活成功教程官网_激活成功教程软件资源

    软件激活成功教程官网_激活成功教程软件资源[转]国内软件激活成功教程下载网站列表!Postedon2005-04-2511:17Laser.NET阅读(872)评论(1)编辑收藏国内最有信誉的激活成功教程下载网站,总能让你有意外收获。18DD资源中心:http://www.18dd.com7年:http://www.7year.com/热战软件园:http://soft.rezhan.comwqsky:http…

    2022年10月11日
  • Android的px、dp和sp

    Android的px、dp和sppx:即像素,1px代表屏幕上一个物理的像素点;偶尔用到px的情况,是需要画1像素表格线或阴影线的时候。dp:这个是最常用但也最难理解的尺寸单位。它与“像素密度”密切相

    2021年12月25日

发表回复

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

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