JRTypedStrings 1.1.0

JRTypedStrings 1.1.0

测试已测试
语言语言 SwiftSwift
许可 MIT
发布日期最后发布2017年10月
SwiftSwift 版本3.0
SPM支持 SPM

Panagiotis Sartzetakis 维护。



JRTypedStrings




JRTypedStrings 提供了一个方便的 API,允许您以类型安全的方式访问 UIViewControllersUIViewsUITableViewCellsUICollectionViewCells。它还消除了当标识符与类名相同时使用字符串标识符的需要。

关于

大多数时候,例如,UITableViewCell 的标识符与类名相匹配。当前用于访问 UITableViewCell 的 API 比较繁琐,它涉及到字符串标识符,这可能会很容易引入错误。此外,我们还需要隐式转换为所需的 类型,这可能导致不安全且难以维护的代码库。这就是 JRTypedStrings 发挥作用的地方,它提供了一系列扩展,有助于您编写更优雅和类型安全的代码。

要求

  • iOS 8+
  • Swift 4.0
  • Xcode 9

入门

import JRTypedStrings

示例

UITableViewCell

之前,您必须做如下操作

override func viewDidLoad() {
    super.viewDidLoad()
    let nib = UINib(nibName: "CustomNibTableViewCell", bundle: nil)
    tableView.register(nib, forCellReuseIdentifier: "CustomNibTableViewCell")
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustomNibTableViewCell", for: indexPath) as! CustomNibTableViewCell

    return cell
}

现在您可以这样操作

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.ts.registerNibCell(CustomNibTableViewCell.self)
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: CustomNibTableViewCell = tableView.ts.dequeueReusableCell(for: indexPath)

    return cell
}

UICollectionViewCell

之前,您必须做如下操作

override func viewDidLoad() {
    super.viewDidLoad()
    let nib = UINib(nibName: "CustomNibCollectionViewCell", bundle: nil)
    collectionView.register(nib, forCellReuseIdentifier: "CustomNibCollectionViewCell")

    let supplementaryViewNib = UINib(nibName: "CustomCollectionViewSupplementaryReusableView", bundle: nil)
    collectionView?.register(supplementaryViewNib, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "CustomCollectionViewSupplementaryReusableView")

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomNibCollectionViewCell", for: indexPath) as! CustomNibCollectionViewCell

    return cell
}

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "CustomCollectionViewSupplementaryReusableView", for: indexPath) as! CustomCollectionViewSupplementaryReusableView

    return reusableView
}

现在您可以这样操作

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView!.ts.registerNibCell(CustomCollectionViewCell.self)
    collectionView!.ts.registerNibForSupplementaryView(CustomCollectionViewSupplementaryReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader)

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell: CustomCollectionViewCell = collectionView.ts.dequeueReusableCell(for: indexPath)

    return cell
}

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    let reusableView: CustomCollectionViewSupplementaryReusableView = collectionView.ts.dequeueReusableSupplementaryView(ofKind: kind, for: indexPath)

    return reusableView
}

UIViewController

之前,您必须做如下操作

let collectionViewController = self.storyboard!.instantiateViewController(withIdentifier: "CustomCollectionViewController") as! CustomCollectionViewController

现在您可以这样操作

let collectionViewController: CustomCollectionViewController = self.storyboard!.ts.instantiate()

从 nib 中创建 UIView

let simpleView: SimpleView = SimpleView.loadFromNib()

更多示例可在 Example/ 中找到。

许可

JRTypedStringsMIT 许可 下提供。

版权 © 2017 年至今 Panagiotis Sartzetakis