From f60dac7d6bb1cba69faac83f2286fef23b647f97 Mon Sep 17 00:00:00 2001 From: Carlo Eugster Date: Sat, 20 Jan 2018 08:06:19 +0100 Subject: [PATCH] Fixed objective-c compatibility (#21) --- .../Extensions/NSBundle+Plist.swift | 6 +- .../NSDictionary+LicenseItems.swift | 2 +- .../LicensesViewController.h | 2 - .../LicensesViewController.swift | 93 ++++++++++--------- 4 files changed, 51 insertions(+), 52 deletions(-) diff --git a/LicensesViewController/Extensions/NSBundle+Plist.swift b/LicensesViewController/Extensions/NSBundle+Plist.swift index cdc6a26..660ea88 100644 --- a/LicensesViewController/Extensions/NSBundle+Plist.swift +++ b/LicensesViewController/Extensions/NSBundle+Plist.swift @@ -11,12 +11,12 @@ import Foundation // MARK: - NSBundle + Plist extension Bundle { - + /** Loads a plist file from the bundle and returns it's contents as a NSDictionary - + - parameter resourceName: The name of the plist. - + - returns: A NSDictionary representation of the plist. */ func loadPlist(_ resourceName: String) -> NSDictionary? { diff --git a/LicensesViewController/Extensions/NSDictionary+LicenseItems.swift b/LicensesViewController/Extensions/NSDictionary+LicenseItems.swift index 8d81966..8710269 100644 --- a/LicensesViewController/Extensions/NSDictionary+LicenseItems.swift +++ b/LicensesViewController/Extensions/NSDictionary+LicenseItems.swift @@ -11,7 +11,7 @@ import Foundation // MARK: - :NSDictionary + CreditItem extension NSDictionary { - + func toLicenseItems() -> Array { var resultArray = Array() if let licensesDicts = self["PreferenceSpecifiers"] as? NSArray { diff --git a/LicensesViewController/LicensesViewController.h b/LicensesViewController/LicensesViewController.h index 973b88d..e472426 100644 --- a/LicensesViewController/LicensesViewController.h +++ b/LicensesViewController/LicensesViewController.h @@ -15,5 +15,3 @@ FOUNDATION_EXPORT double LicensesViewControllerVersionNumber; FOUNDATION_EXPORT const unsigned char LicensesViewControllerVersionString[]; // In this header, you should import all the public headers of your framework using statements like #import - - diff --git a/LicensesViewController/LicensesViewController.swift b/LicensesViewController/LicensesViewController.swift index 598c2e8..9b70152 100644 --- a/LicensesViewController/LicensesViewController.swift +++ b/LicensesViewController/LicensesViewController.swift @@ -12,31 +12,32 @@ import UIKit /// ViewController that displays Settings.bundle style credist. open class LicensesViewController : UIViewController { - + /// The tableView let tableView = UITableView(frame: CGRect.zero, style: .grouped) - + /// The tableView's UITableViewDataSource. var dataSource: LicensesDataSource! - + /// Boolean tracking if the constrains have been setup. fileprivate var didSetupConstraints = false - + fileprivate let reuseIdentifier = "LicenseCell" - + open override func viewDidLoad() { super.viewDidLoad() - + title = NSLocalizedString("Acknowledgements", comment: "Acknowledgements") - + tableView.register(LicenseCell.classForCoder(), forCellReuseIdentifier: reuseIdentifier) tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 68.0 tableView.translatesAutoresizingMaskIntoConstraints = false - + view.addSubview(tableView) + view.setNeedsUpdateConstraints() } - + open override func updateViewConstraints() { if(!didSetupConstraints) { view.addConstraints( @@ -53,34 +54,34 @@ open class LicensesViewController : UIViewController { views: ["tableView" : tableView] ) ) - + didSetupConstraints = true } super.updateViewConstraints() } - + /** Loads the contents of a Settings.bundle style plist into the tableView - + - parameter bundle: The bundle containing the plist - parameter resourceName: The resource name of the plist */ - open func loadPlist(_ bundle: Bundle, resourceName: String) { + @objc open func loadPlist(_ bundle: Bundle, resourceName: String) { if let plistDict = bundle.loadPlist(resourceName) { loadPlist(plistDict) } } - + /** Loads the contents of a Settings.bundle style plist dictionary into the tableView - + - parameter dictionary: A Settings.bundle style NSDictionary */ - open func loadPlist(_ dictionary: NSDictionary) { + @objc open func loadPlist(_ dictionary: NSDictionary) { let items = dictionary.toLicenseItems() setDataSource(items) } - + /** Initializes the tableView's dataSource. */ @@ -97,53 +98,53 @@ open class LicensesViewController : UIViewController { // MARK: - LicenseCell class LicenseCell : UITableViewCell { - + /// The title label of the cell. let titleLabel = UILabel() - + /// The body label of the cell. let bodyLabel = UILabel() - + /// Boolean tracking if the constrains have been setup. fileprivate var didSetupConstraints = false - + /** Initializes a new LicenseCell - + - parameter style: This property is ignoed. - parameter reuseIdentifier: A string used to identify the cell object if it is to be reused for drawing multiple rows of a table view. Pass nil if the cell object is not to be reused. You should use the same reuse identifier for all cells of the same form. - + - returns: An initialized UITableViewCell object or nil if the object could not be created. */ override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) - + selectionStyle = .none - + titleLabel.textColor = UIColor.black titleLabel.translatesAutoresizingMaskIntoConstraints = false titleLabel.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline) titleLabel.lineBreakMode = .byTruncatingTail titleLabel.numberOfLines = 1 contentView.addSubview(titleLabel) - + bodyLabel.textColor = UIColor.darkGray bodyLabel.translatesAutoresizingMaskIntoConstraints = false bodyLabel.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.footnote) bodyLabel.lineBreakMode = .byWordWrapping bodyLabel.numberOfLines = 0 contentView.addSubview(bodyLabel) - + setNeedsUpdateConstraints() updateConstraintsIfNeeded() } - + required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } - + override func updateConstraints() { if(!didSetupConstraints) { let noLayoutOption = NSLayoutFormatOptions(rawValue: 0) @@ -151,7 +152,7 @@ class LicenseCell : UITableViewCell { "titleLabel" : titleLabel, "bodyLabel" : bodyLabel ] - + contentView.addConstraints( NSLayoutConstraint.constraints(withVisualFormat: "H:|-[titleLabel]-|", options: noLayoutOption, @@ -172,7 +173,7 @@ class LicenseCell : UITableViewCell { ) didSetupConstraints = true } - + super.updateConstraints() } } @@ -180,27 +181,27 @@ class LicenseCell : UITableViewCell { // MARK: - LicensesDataSource /// A wrapper around `UITableViewDataSource` -class LicensesDataSource: NSObject, UITableViewDataSource { - +@objc class LicensesDataSource: NSObject, UITableViewDataSource { + /// A closure for configuring cells. typealias LicenseCellConfigClosure = (_ cell: LicenseCell, _ item: LicenseItem) -> () - + /// Closure that is called to configure cells. let configureCell: LicenseCellConfigClosure! - + /// The reuse identifier of the tableView's cells. let reuseIdentifier: String! - + /// The models represented by the dataSource. let items: Array - + /** Initializes a new `dataSource` with items and a configuration closure. - + - parameter reuseIdentifier: The `reuseIdentifier` of cells in the tableView. - parameter items: The license items representing the data in the tableView. - parameter config: The closure that will be called to configure cells. - + - returns: An initialized `dataSource` object. */ init(reuseIdentifier: String, items: Array, configureCell: @escaping LicenseCellConfigClosure) { @@ -208,28 +209,28 @@ class LicensesDataSource: NSObject, UITableViewDataSource { self.reuseIdentifier = reuseIdentifier self.items = items; } - + func numberOfSections(in tableView: UITableView) -> Int { return items.count } - + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } - + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier) as? LicenseCell - + let item = items[(indexPath as NSIndexPath).section] configureCell?(cell!, item) return cell! } - + /** Returns a `LicenseItem` at an `indexPath`. - + - parameter indexPath: The `indexPath` of the item. - + - returns: The `LicenseItem` at the given `indexPath`. */ func itemAtIndexPath(_ indexPath: IndexPath) -> LicenseItem {