<commit_before>//
//  ReusableView.swift
//  ReusableViews
//
//  Created by <NAME> on 5/11/17
//
//

import Foundation

/// A reusable, recycled view.
public protocol ReusableView: class {
  /// Reuse identifier for a recycled view.
  static var defaultReuseIdentifier: String { get }
}

public extension ReusableView where Self: UIView {
  /// The reuse identifier for the class.
  /// The same as the class name.
  static var defaultReuseIdentifier: String {
    return String(describing: self)
  }
}

/// A view controller or other object that can be instantiated from the storyboard
public protocol StoryboardCompatibleView: class {
  static var storyboardIdentifier: String { get }
}

public extension StoryboardCompatibleView where Self: UIViewController {
  /// Defaults to the same name as the class
  static var storyboardIdentifier: String {
    return String(describing: self)
  }
}

/// A view loadable from a nib
public protocol NibLoadableView: class {
  /// Name for the nib
  static var nibName: String { get }
}

public extension NibLoadableView where Self: UIView {
  /// Defaults to the same name as the class
  static var nibName: String {
    return String(describing: self)
  }

  /// Instantiates and loads from nib a view that conforms to NibLoadableView
  ///
  /// Can fail if the nib cannot be loaded from the view:
  ///  - Usually caused by not matching the name of the nib file with the name of the view 
  ///  - Similar & overly long file names confuse Xcode. For example, AdHocNibLoadableView and AdHocNibLoadableViewController will confuse 
  ///    Xcode and prevent it from instantiating files correctly.
  ///
  /// - Parameters:
  ///   - viewIndex: index of the view, by default 0 
  ///     Currently, additional indexes are unsupported due to the requirement that nibName is the same as className
  ///   - owner: by default nil
  ///   - options: by default nil
  /// - Returns: an instantiated view
  static func create(viewIndex: Int = 0, owner: Any? = nil, options: [AnyHashable: Any]? = nil) -> Self {
    let bundle = Bundle(for: Self.self)
    guard let view = bundle.loadNibNamed(Self.nibName, owner: owner, options: options as? [UINib.OptionsKey : Any])?[viewIndex] as? Self else {
      fatalError("Could not instantiate \(Self.self) from nib -- have you conformed to the NibLoadable protocol?")
    }
    return view
  }
}

extension UIViewController: StoryboardCompatibleView { }
extension UITableViewCell: ReusableView { }
extension UICollectionReusableView: ReusableView { }
extension UITableViewHeaderFooterView: ReusableView { }
<commit_msg>Split guard in multiple lines<commit_after>//
//  ReusableView.swift
//  ReusableViews
//
//  Created by <NAME> on 5/11/17
//
//

import Foundation

/// A reusable, recycled view.
public protocol ReusableView: class {
  /// Reuse identifier for a recycled view.
  static var defaultReuseIdentifier: String { get }
}

public extension ReusableView where Self: UIView {
  /// The reuse identifier for the class.
  /// The same as the class name.
  static var defaultReuseIdentifier: String {
    return String(describing: self)
  }
}

/// A view controller or other object that can be instantiated from the storyboard
public protocol StoryboardCompatibleView: class {
  static var storyboardIdentifier: String { get }
}

public extension StoryboardCompatibleView where Self: UIViewController {
  /// Defaults to the same name as the class
  static var storyboardIdentifier: String {
    return String(describing: self)
  }
}

/// A view loadable from a nib
public protocol NibLoadableView: class {
  /// Name for the nib
  static var nibName: String { get }
}

public extension NibLoadableView where Self: UIView {
  /// Defaults to the same name as the class
  static var nibName: String {
    return String(describing: self)
  }

  /// Instantiates and loads from nib a view that conforms to NibLoadableView
  ///
  /// Can fail if the nib cannot be loaded from the view:
  ///  - Usually caused by not matching the name of the nib file with the name of the view 
  ///  - Similar & overly long file names confuse Xcode. For example, AdHocNibLoadableView and AdHocNibLoadableViewController will confuse 
  ///    Xcode and prevent it from instantiating files correctly.
  ///
  /// - Parameters:
  ///   - viewIndex: index of the view, by default 0 
  ///     Currently, additional indexes are unsupported due to the requirement that nibName is the same as className
  ///   - owner: by default nil
  ///   - options: by default nil
  /// - Returns: an instantiated view
  static func create(viewIndex: Int = 0, owner: Any? = nil, options: [AnyHashable: Any]? = nil) -> Self {
    let bundle = Bundle(for: Self.self)
    guard let nib = bundle.loadNibNamed(Self.nibName, owner: owner, options: options as? [UINib.OptionsKey : Any]) else {
        fatalError("nib file \(Self.nibName) is missing")
    }
    guard let view = nib[viewIndex] as? Self else {
        fatalError("Could not instantiate \(Self.self) from nib -- have you conformed to the NibLoadable protocol?")
    }
    return view
  }
}

extension UIViewController: StoryboardCompatibleView { }
extension UITableViewCell: ReusableView { }
extension UICollectionReusableView: ReusableView { }
extension UITableViewHeaderFooterView: ReusableView { }

{'fraction_non_alphanumeric': 0.08625235404896422, 'fraction_numerical': 0.0026365348399246705, 'mean_word_length': 3.2761674718196456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4612894', 'n_tokens_mistral': 1572, 'n_tokens_neox': 1461, 'n_words': 602}