JRTypedStrings 1.1.0

JRTypedStrings 1.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Panagiotis Sartzetakis.



JRTypedStrings




JRTypedStrings provides a convenience API that allows you to access UIViewControllers, UIViews, UITableViewCells and UICollectionViewCells in a type-safe way. It also eliminates the use of string identifiers when the identifier is the same as the class name.

About

Most of the times the identifier of a UITableViewCell for example matches the class name. The current API for accessing a UITableViewCell is quite cumbersome and it involves string identifiers which can easily introduce typos. Also we will have to implicitly cast to the required type and that can result to unsafe and hard to maintain codebase. This is where the JRTypedStrings comes to play, it provides a set of extensions which will help you write more elegant and type-safe code.

Requirements

  • iOS 8+
  • Swift 4.0
  • Xcode 9

Getting Started

import JRTypedStrings

Examples

UITableViewCell

Before, you had to do the following:

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
}

Now you can do:

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

Before, you had to do the following:

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
}

Now you can do:

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

Before, you had to do the following:

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

Now you can do:

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

UIView from nib

let simpleView: SimpleView = SimpleView.loadFromNib()

There are a few more example available at Example/.

Licence

JRTypedStrings is being provided under MIT Licence.

Copyright © 2017-present Panagiotis Sartzetakis