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


相关推荐

  • C# 调用java Webservice「建议收藏」

    C# 调用java Webservice「建议收藏」usingMicrosoft.CSharp;usingSystem;usingSystem.CodeDom;usingSystem.CodeDom.Compiler;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Net;usingSystem.Text;us

    2022年10月16日
    2
  • 静态分析工具之-AXMLPrinter2.jar的使用方法

    静态分析工具之-AXMLPrinter2.jar的使用方法

    2021年10月1日
    38
  • 《局域网聊天工具》项目总结

    《局域网聊天工具》项目总结

    2021年8月25日
    55
  • android 打开相册_安卓系统照片在哪个存储文件中

    android 打开相册_安卓系统照片在哪个存储文件中在GoogleNexus7(Version4.4.2)平板出现之前,Intent.ACTION_GET_CONTENT打开相册会返回如下形式的Uri: content://media/external/images/media/3951,  使用ContentResolver查询MediaStore.Images.Media.DATA就可以找文件

    2022年9月14日
    2
  • Matlab画图线型、符号及颜色设置

    Matlab画图线型、符号及颜色设置在matlab中线条的属性主要有:Color:颜色LineStyle:线型LineWidth:线宽Marker:标记点的形状MarkerFaceColor:标记点填充颜色MarkerEdgeColor:标记点边缘颜色MarkerSize:标记点大小举例x=[-2*pi:0.01:2*pi];y1=sin(x);y2=cos(x);figure;%打开一个画板%画两条线,返回的是这两条线的句柄,h是一个包含两个句柄的数组h=plo

    2022年5月31日
    41
  • 用Python读取CSV文件的5种方式

    用Python读取CSV文件的5种方式典型的数据集stocks.csv:一个股票的数据集,其实就是常见的表格数据。有股票代码,价格,日期,时间,价格变动和成交量。这个数据集其实就是一个表格数据,有自己的头部和身体。第一招:简单的读取我们先来看一种简单读取方法,先用csv.reader()函数读取文件的句柄f生成一个csv的句柄,其实就是一个迭代器,我们看一下这个reader的源码:喂给reader一个可迭代对象或者是文件的object,然后返回一个可迭代对象。首先读取csv文件,然后用csv.reader生成一个csv迭代器

    2022年7月21日
    12

发表回复

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

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