Swiftility 0.13.0

Swiftility 0.13.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布时间最后发布日期2017年9月
SwiftSwift 版本3.0
SPM支持使用 SPM

Allan Bto 维护。



Swiftility 0.13.0

  • 作者:
  • Allan Bto

Swiftility

用于各种项目的框架分组工具

注意:此框架仅仅是工具的集合,以及一些“方便的”功能。
请随意选择适合您项目的功能和示例代码,而不是包含整个 pod。

安装

Swiftility 通过 CocoaPods 提供。要安装,只需将以下行添加到您的 Podfile:

pod "Swiftility"

使用

import Swiftility

特性

MVVM

查看我使用 Swiftility 编写的 MVVM 实现示例的 链接

核心

动态

轻量级 Swift 绑定

struct Dynamic<T> {
    var value: T

    // ...

    init(_ value: T)

    // ...

    func bind(listener: T -> Void)

    // ...
}

/// Usage example

var email: Dynamic<String>("")

email.bind { value in
    print("Email is now: \(value)")
}

email.value = "[email protected]"
// prints "Email is now: [email protected]"

大中央调度

便捷使用 GCD 方法

async()

async {
    // Asynchronous code

    async_main {
        // Main thread code
    }
}

after()

after(2.5) {
    // Executed after 2.5 seconds
}

once()

once("someToken") {
    // Executed only once for the given token
}

debounce()

let debouncedFunction = debounced(0.5) { someVarOrVoid in
    // Executed only every 0.5 seconds
}

debouncedFunction(someVarOrVoid)

类型安全

Storyboard

/// Define Storyboard names

extension UIStoryboard.Name
{
    static let settings = UIStoryboard.Name(name: "Settings")
}

/// Define your view controller built in the `Settings` storyboard
// "SettingsViewController" should be the identifier in the storyboard (same as class name)

class SettingsViewController: UIViewController, FromStoryboard
{
    static let storyboardName: UIStoryboard.Name = .settings
}

/// Now you can do this

let settingsVC = SettingsViewController.instantiateFromStoryboard()

Nib

// "SettingsViewController.xib" should be the file containing SettingsViewController

class SettingsViewController: UIViewController, FromNib { ... }

// "MyView.xib" should be the file containing MyView

class MyView: UIView, FromNib { ... }

/// Now you can do this

let settingsVC = SettingsViewController.instantiateFromNib()

let myView = MyView.instantiateFromNib()

TableView

/// Define cell defined in "MyCell.xib"

class MyCell: UITableViewCell, FromNib { ... }

/// Register cell

tableView.register(MyCell.self)

/// Later

let cell = tableView.dequeueReusableCell() as MyCell

CollectionView

/// Define cell defined in "MyCell.xib"

class MyCell: UICollectionViewCell, FromNib { ... }

collectionView.register(MyCell.self)

/// Define custom view defined in "MyCustomView.xib"

class MyCustomView: UICollectionReusableView, FromNib { ... }

collectionView.register(MyCustomView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader)

/// Later

let supView: MyCustomView = collectioView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, for: someIndexPath)

let cell = collectioView.dequeueReusableCell(for: someIndexPath) as MyCell

扩展

参见 扩展页面

作者

Allan Barbato,[email protected]

许可证

Swiftility可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。