SGSwiftyBind 1.0.2

SGSwiftyBind 1.0.2

Manny Guerrero 维护。



  • 作者
  • Manny Guerrero

SGSwiftyBind

基于事件编程的轻量级方法

Build Status codecov Swift 4.0 CocoaPods Carthage DUB platform standard-readme compliant

目录

背景

在我使用类似于 Model-View-Viewmodel 的架构开发应用程序时,需要知道对象中的值何时发生变化,或者只想有一个基于通知的 API,我发现使用诸如 NotificationCenter 这样的 API 很难写很好的单元测试以验证执行是否发生。此外,使用这些现有 API 也会导致不使用单向数据流的坏习惯。

安装

CocoaPods

您可以使用CocoaPods通过将其添加到您的Podfile中安装SGSwiftyBind

platform :ios, '9.0'
use_frameworks

target 'MyApp' do
    pod 'SGSwiftyBind'
end

Carthage

您可以通过将SGSwiftyBind添加到您的Cartfile中使用Carthage进行安装

github "eman6576/SGSwiftyBind"

Swift Package Manager

您可以通过在您的Package.swift文件中添加适当描述来使用Swift Package Manager安装SGSwiftyBind

import PackageDescription

let package = Package(
    name: "YOUR_PROJECT_NAME",
    dependencies: [
        .package(url: "https://github.com/eman6576/SGSwiftyBind.git", from: "1.0.0")
    ],
    targets: [
        .target(
            name: "YOUR_TARGET_NAME",
            dependencies: [
                "SGSwiftyBind"
            ])
    ]
)

使用方法

注意:本存储库包含一个示例目录,其中包含不同示例,说明如何使用此库。我们建议您尝试使用示例。还可以查看单元测试,以查看库的实际操作。

初始化

要访问可用数据类型,请将SGSwiftyBind导入您的项目,如下所示

import SGSwiftyBind

功能

SGSwiftyBind使用泛型来存储不同数据类型

let intBind: SGSwiftyBind<Int> = SGSwiftyBind(1)
let doubleBind: SGSwiftyBind<Double> = SGSwiftyBind(1.2)
let stringBind: SGSwiftyBind<String> = SGSwiftyBind("Hello World")

您甚至可以自定义自己的类型

protocol Animal {
    var age: Int { get }

    init(age: Int)

    func makeSound()

    func increaseAge()
}

struct Dog: Animal {
    var age: Int

    init(age: Int) {
        self.age = age
    }

    func makeSound() {
        print("Bark")
    }

    func increaseAge() {
        age += 1
    }
}

let dog = Dog(age: 3)
let animalBind: SGSwiftyBind<Animal> = SGSwiftyBind(dog)

使用SGSwiftyBind的另一个非常好的用例是知道变量何时变化或发生某些事件。让我们修改Animal协议以使用SGSwiftyBind

protocol Animal {
    var age: SGSwiftyBindInterface<Int> { get }

    init(age: Int)

    func makeSound()

    func increaseAge()
}

struct Dog: Animal {
    var age: SGSwiftyBindInterface<Int> {
        return ageBind.interface
    }

    private let ageBind: SGSwiftyBind<Int>

    init(age: Int) {
        ageBind = SGSwiftyBind(age)
    }

    func makeSound() {
        print("Bark")
    }

    func increaseAge() {
        ageBind.value += 1
    }
}

我们将Animal修改为在age属性上具有绑定接口。我们的Dog类需要更新它的age属性以返回一个绑定接口,并添加一个类型为SGSwiftyBind的私유private binder,称为ageBind。现在让我们使用我们的Dog对象。

let dog = Dog(age: 3)

dog.age.bind({ (newAge) in
    print("This is the dog's new age: \(newAge)")
}, for: self)

dog.makeSound()
dog.increaseAge()

在示例中,我们创建了一个Dog实例。然后,我们在age属性上注册了一个绑定,以便在值改变时我们可以获取新值并对其进行某些操作。在我们的例子中,我们将值打印到控制台。

Dog实例上的age属性是SGSwiftyBindInterface类型,而不是SGSwiftyBind。背后的想法是防止外部更改ageBind的状态。以下是一个示例

let currentAge = dog.age.value // We can retrieve the current value of age
dog.age.value = currentAge     // The complier would give us an error stating that `age.value` is a get-only property

一旦在Dog实例上完成对bind器的使用,我们应该做一些清理并像这样做:

dog.age.unbind(for: self)

我们还可以获取已经注册特定bind器的监视器的当前数量。

let observerCount = dog.age.observerCount

参与贡献

参与贡献文件

接受PR。

小贴士:编辑Readme时,请遵守standard-readme规范

维护者

Manny Guerrero Twitter Follow GitHub followers

许可证

MIT © Manny Guerrero。