ObserveIt 1.0.0

ObserveIt 1.0.0

Julian Lima 维护。



ObserveIt 1.0.0

  • 作者:
  • Julian Lima

ObserveIt

观察者模式的简单实现

代码示例

首先需要导入项目

import ObserveIt

然后,您必须从 Observable 类继承并将属性设置为 dynamic,如下所示:

enum ExampleEnum: String {
    case property = "property"
}

class Example: Observable {
	@objc dynamic var property: String = "Hello, World!"
}

绑定观察者将在 Example 对象的属性更新时执行回调

//create the observable
var ex = Example()

//self is the object who listen the changes of the property
//key is the name of the property who is observed
//asyncCallback will avoid to listen other changes before you unblock() the property for this observer
ex.observe(with: self, key: ExampleEnum.property.rawValue, callback: { newValue in
	    guard let value = newValue.value as? String else { return }
            print(value)
})
	
//to test this
ex.property = "modify the value of the property observed"

//to stop listen
ex.ignore(with: self, key: ExampleEnum.property.rawValue) //this will remove the observer from the list that bind observer with the observable

绑定观察者将在 Example 对象的属性更新并执行 next() 回调时执行

//create the observable
var ex = Example()

//self is the object who listen the changes of the property
//key is the name of the property who is observed
//asyncCallback will avoid to listen other changes before you unblock() the property for this observer
ex.observe(with: self, key: ExampleEnum.property.rawValue, asyncCallback: false, callback: { newValue  in
    guard let value = newValue.value as? String else { return }
    APIHelper.shared.getInfo(text: value, completionHandler: { (result) in
	print(result)
	newValue.unblock()//unlock the property to continue listen changes
    })
})

//to test this
ex.property = "modify the value of the property observed"

//this will remove the observer from the list that bind observer with the observable
ex.ignore(with: self, key: ExampleEnum.property.rawValue) 

CocoaPods

platform :ios, '9.0'

target 'AppName' do

  use_frameworks!
  
    pod 'ObserveIt'

end

许可

MIT 许可证

版权所有 (c) 2018 Julian Lima

在此条件下,免费授予任何获取此软件及其相关文档副本(“软件”)的人权,在不受限制的情况下处理该软件,包括但不限于以下权利...

以上版权声明和本许可声明应包含在软件的副本或主要部分中。

本软件按“现状”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、适用于特定用途和侵权性保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他法律责任负责,无论这些责任是根据合同、侵权或其他法律产生,无论这些责任源于、由或与该软件或其使用或其他任何方式有关。