Swift中实现Observable机制

Posted Cocos2der

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Swift中实现Observable机制相关的知识,希望对你有一定的参考价值。

猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!
原文地址: http://blog.csdn.net/cocos2der/article/details/51917539

今天给别人讲个Observable的实现和使用场景,结合Observable-Swift github: https://github.com/slazyk/Observable-Swift 讲了半天貌似还没有特别明白,故写了个简易的实现,讲述了下Observable属性监控机制。

//: Playground - noun: a place where people can play

import UIKit
import Foundation

// MARK: - Observable
class Observable<T> 
    // 定义block结构
    typealias Observer = T -> Void

    // 申明一个block,用于数据改变的执行
    private var observer: Observer?

    // 数据发生变更,则通过observer告知
    var value: T 
        didSet 
            observer?(value)
        
    

    init(_ v: T) 
        value = v
    

    func observe(observer: Observer?) 
        self.observer = observer
        observer?(value)
    


// MARK: - People
struct PeopleModel 
    let firstName: Observable<String>
    let lastName: Observable<String>

    init(firstName: String, lastName: String) 
        self.firstName = Observable(firstName)
        self.lastName = Observable(lastName)
    


// MARK: - Test

// test1
let people = PeopleModel(firstName: "sunny", lastName: "liu")
people.firstName.observe 
    newValue in
    print("firstName changed: \\(newValue)")

people.lastName.observe 
    print("lastName changed: \\($0)")

people.firstName.value = "sunny2"
people.lastName.value = "liu2"

// test2
class House 
    let lableHouseName =  UILabel()

    init() 

    

    var people: PeopleModel? 
        didSet 
            people?.firstName.observe
                [unowned self] in
                self.lableHouseName.text = $0
            
        
    

这样貌似容易理解了,O(∩_∩)O哈哈~

以上是关于Swift中实现Observable机制的主要内容,如果未能解决你的问题,请参考以下文章

Swift3 如何像在 android 中一样在 iOS 中实现底部工作表

如何在实际应用中实现 CallKit?

在 UICollectionViewController (Swift 4) 中实现 UISearchBarController?

swift 样本机协议在swift中实现

swift 在swift中实现整数结构

如何在 Swift 中实现单例类