From d8d323b47011065a47028f37cc069bb9d2c1f0cd Mon Sep 17 00:00:00 2001 From: Sam Soffes Date: Tue, 14 Jun 2016 15:17:36 -0700 Subject: [PATCH] Finish Swift 3 conversion --- Example/CustomTableViewCell.swift | 21 +++++++++++--------- Example/NibTableViewCell.swift | 4 ++-- Example/ViewController.swift | 26 ++++++++++++------------- Example/WindowController.swift | 6 +++--- Static.xcodeproj/project.pbxproj | 32 +++++++++++++++---------------- Static/ButtonCell.swift | 2 +- Static/CellType.swift | 6 +++--- Static/DataSource.swift | 10 +++++----- Static/Row.swift | 4 ++-- Static/Section.swift | 8 ++++---- Static/SubtitleCell.swift | 2 +- Static/Value1Cell.swift | 2 +- Static/Value2Cell.swift | 2 +- 13 files changed, 64 insertions(+), 61 deletions(-) diff --git a/Example/CustomTableViewCell.swift b/Example/CustomTableViewCell.swift index 4e8277f..62f19f3 100644 --- a/Example/CustomTableViewCell.swift +++ b/Example/CustomTableViewCell.swift @@ -1,14 +1,14 @@ import UIKit import Static -final class CustomTableViewCell: UITableViewCell, CellType { +final class CustomTableViewCell: UITableViewCell, Cell { // MARK: - Properties private lazy var centeredLabel: UILabel = { let label = UILabel() - label.textAlignment = .Center - label.textColor = .whiteColor() + label.textAlignment = .center + label.textColor = .white() label.translatesAutoresizingMaskIntoConstraints = false return label }() @@ -18,14 +18,17 @@ final class CustomTableViewCell: UITableViewCell, CellType { override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) - contentView.backgroundColor = .grayColor() + contentView.backgroundColor = .gray() contentView.addSubview(centeredLabel) - let views = ["centeredLabel": centeredLabel] - var constraints: [NSLayoutConstraint] = NSLayoutConstraint.constraintsWithVisualFormat("|-[centeredLabel]-|", options: [], metrics: nil, views: views) - constraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|-[centeredLabel]-|", options: [], metrics: nil, views: views) - NSLayoutConstraint.activateConstraints(constraints) + NSLayoutConstraint.activate([ + centeredLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8), + centeredLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 8), + centeredLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor), + centeredLabel.leadingAnchor.constraint(greaterThanOrEqualTo: contentView.leadingAnchor, constant: 8), + centeredLabel.trailingAnchor.constraint(lessThanOrEqualTo: contentView.trailingAnchor, constant: -8) + ]) } required init?(coder aDecoder: NSCoder) { @@ -35,7 +38,7 @@ final class CustomTableViewCell: UITableViewCell, CellType { // MARK: - CellType - func configure(row row: Row) { + func configure(row: Row) { centeredLabel.text = row.text } } diff --git a/Example/NibTableViewCell.swift b/Example/NibTableViewCell.swift index 86ee10b..4ffecb2 100644 --- a/Example/NibTableViewCell.swift +++ b/Example/NibTableViewCell.swift @@ -1,7 +1,7 @@ import UIKit import Static -final class NibTableViewCell: UITableViewCell, CellType { +final class NibTableViewCell: UITableViewCell, Cell { // MARK: - Properties @@ -14,7 +14,7 @@ final class NibTableViewCell: UITableViewCell, CellType { return UINib(nibName: String(self), bundle: nil) } - func configure(row row: Row) { + func configure(row: Row) { centeredLabel.text = row.text } } diff --git a/Example/ViewController.swift b/Example/ViewController.swift index 629d92d..5cafdd4 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -7,7 +7,7 @@ class ViewController: TableViewController { private let customAccessory: UIView = { let view = UIView(frame: CGRect(x: 0, y: 0, width: 32, height: 32)) - view.backgroundColor = .redColor() + view.backgroundColor = .red() return view }() @@ -15,7 +15,7 @@ class ViewController: TableViewController { // MARK: - Initializers convenience init() { - self.init(style: .Grouped) + self.init(style: .grouped) } @@ -41,15 +41,15 @@ class ViewController: TableViewController { ], footer: "This is a section footer."), Section(header: "Accessories", rows: [ Row(text: "None"), - Row(text: "Disclosure Indicator", accessory: .DisclosureIndicator), - Row(text: "Detail Disclosure Button", accessory: .DetailDisclosureButton({ [unowned self] in + Row(text: "Disclosure Indicator", accessory: .disclosureIndicator), + Row(text: "Detail Disclosure Button", accessory: .detailDisclosureButton({ [unowned self] in self.showAlert(title: "Detail Disclosure Button") })), - Row(text: "Checkmark", accessory: .Checkmark), - Row(text: "Detail Button", accessory: .DetailButton({ [unowned self] in + Row(text: "Checkmark", accessory: .checkmark), + Row(text: "Detail Button", accessory: .detailButton({ [unowned self] in self.showAlert(title: "Detail Button") })), - Row(text: "Custom View", accessory: .View(customAccessory)) + Row(text: "Custom View", accessory: .view(customAccessory)) ], footer: "Try tapping the ⓘ buttons."), Section(header: "Selection", rows: [ Row(text: "Tap this row", selection: { [unowned self] in @@ -62,10 +62,10 @@ class ViewController: TableViewController { ]), Section(header: "Editing", rows: [ Row(text: "Swipe this row", editActions: [ - Row.EditAction(title: "Warn", backgroundColor: .orangeColor(), selection: { [unowned self] in + Row.EditAction(title: "Warn", backgroundColor: .orange(), selection: { [unowned self] in self.showAlert(title: "Warned.") }), - Row.EditAction(title: "Delete", style: .Destructive, selection: { [unowned self] in + Row.EditAction(title: "Delete", style: .destructive, selection: { [unowned self] in self.showAlert(title: "Deleted.") }) ]) @@ -76,9 +76,9 @@ class ViewController: TableViewController { // MARK: - Private - private func showAlert(title title: String? = nil, message: String? = "You tapped it. Good work.", button: String = "Thanks") { - let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert) - alert.addAction(UIAlertAction(title: button, style: .Cancel, handler: nil)) - presentViewController(alert, animated: true, completion: nil) + private func showAlert(title: String? = nil, message: String? = "You tapped it. Good work.", button: String = "Thanks") { + let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) + alert.addAction(UIAlertAction(title: button, style: .cancel, handler: nil)) + present(alert, animated: true, completion: nil) } } diff --git a/Example/WindowController.swift b/Example/WindowController.swift index cdd3b03..ecfbdd8 100644 --- a/Example/WindowController.swift +++ b/Example/WindowController.swift @@ -1,8 +1,8 @@ import UIKit -@UIApplicationMain class WindowController: UIResponder { +@UIApplicationMain final class WindowController: UIResponder { var window: UIWindow? = { - let window = UIWindow(frame: UIScreen.mainScreen().bounds) + let window = UIWindow(frame: UIScreen.main().bounds) window.rootViewController = UINavigationController(rootViewController: ViewController()) return window }() @@ -10,7 +10,7 @@ import UIKit extension WindowController: UIApplicationDelegate { - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { window?.makeKeyAndVisible() return true } diff --git a/Static.xcodeproj/project.pbxproj b/Static.xcodeproj/project.pbxproj index 79b744c..2911c88 100644 --- a/Static.xcodeproj/project.pbxproj +++ b/Static.xcodeproj/project.pbxproj @@ -9,13 +9,15 @@ /* Begin PBXBuildFile section */ 21826AAE1B3F51A100AA9641 /* Static.h in Headers */ = {isa = PBXBuildFile; fileRef = 21826AAD1B3F51A100AA9641 /* Static.h */; settings = {ATTRIBUTES = (Public, ); }; }; 21826AB51B3F51A100AA9641 /* Static.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 21826AAA1B3F51A100AA9641 /* Static.framework */; }; - 21826AC91B3F51D000AA9641 /* ButtonCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AC41B3F51D000AA9641 /* ButtonCell.swift */; }; 21826ACA1B3F51D000AA9641 /* Row.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AC51B3F51D000AA9641 /* Row.swift */; }; - 21826ACB1B3F51D000AA9641 /* Section.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AC61B3F51D000AA9641 /* Section.swift */; }; - 21826ACC1B3F51D000AA9641 /* DataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AC71B3F51D000AA9641 /* DataSource.swift */; }; - 21826ACD1B3F51D000AA9641 /* Value1Cell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AC81B3F51D000AA9641 /* Value1Cell.swift */; }; - 21826ACF1B3F534000AA9641 /* Value2Cell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826ACE1B3F534000AA9641 /* Value2Cell.swift */; }; - 21826AD11B3F535A00AA9641 /* SubtitleCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AD01B3F535A00AA9641 /* SubtitleCell.swift */; }; + 21F219601D10B784001EC0F5 /* CellType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36799F981B41C857009A9D16 /* CellType.swift */; }; + 21F219611D10B7A9001EC0F5 /* Value1Cell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AC81B3F51D000AA9641 /* Value1Cell.swift */; }; + 21F219621D10B7B9001EC0F5 /* Value2Cell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826ACE1B3F534000AA9641 /* Value2Cell.swift */; }; + 21F219631D10B7B9001EC0F5 /* SubtitleCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AD01B3F535A00AA9641 /* SubtitleCell.swift */; }; + 21F219641D10B7B9001EC0F5 /* ButtonCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AC41B3F51D000AA9641 /* ButtonCell.swift */; }; + 21F219671D10B7CF001EC0F5 /* Section.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AC61B3F51D000AA9641 /* Section.swift */; }; + 21F219681D10B895001EC0F5 /* DataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21826AC71B3F51D000AA9641 /* DataSource.swift */; }; + 21F219691D10B8E1001EC0F5 /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C8FE9A1B4EECF30004DA5B /* TableViewController.swift */; }; 363129C71B5035520024E339 /* Static.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 21826AAA1B3F51A100AA9641 /* Static.framework */; }; 363129C91B50355E0024E339 /* Static.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 21826AAA1B3F51A100AA9641 /* Static.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 366AB2F31B4EE1A7002C4717 /* RowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 366AB2EF1B4EE1A7002C4717 /* RowTests.swift */; }; @@ -25,8 +27,6 @@ 36748D541B5034EC0046F207 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36748D531B5034EC0046F207 /* ViewController.swift */; }; 36748D591B5034EC0046F207 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 36748D581B5034EC0046F207 /* Assets.xcassets */; }; 36748D5C1B5034EC0046F207 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 36748D5A1B5034EC0046F207 /* LaunchScreen.storyboard */; }; - 36799F991B41C857009A9D16 /* CellType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36799F981B41C857009A9D16 /* CellType.swift */; }; - 36C8FE9B1B4EECF30004DA5B /* TableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C8FE9A1B4EECF30004DA5B /* TableViewController.swift */; }; 39DC804A1BD96BB0001F04CD /* NibTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39DC80491BD96BB0001F04CD /* NibTableViewCell.swift */; }; A706253D1BC81C1400E471EF /* CustomTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = A706253B1BC81C1400E471EF /* CustomTableViewCell.swift */; }; A706253E1BC81C1400E471EF /* NibTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = A706253C1BC81C1400E471EF /* NibTableViewCell.xib */; }; @@ -336,15 +336,15 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 21826ACB1B3F51D000AA9641 /* Section.swift in Sources */, - 21826ACC1B3F51D000AA9641 /* DataSource.swift in Sources */, + 21F219631D10B7B9001EC0F5 /* SubtitleCell.swift in Sources */, + 21F219611D10B7A9001EC0F5 /* Value1Cell.swift in Sources */, 21826ACA1B3F51D000AA9641 /* Row.swift in Sources */, - 21826AC91B3F51D000AA9641 /* ButtonCell.swift in Sources */, - 21826ACF1B3F534000AA9641 /* Value2Cell.swift in Sources */, - 36799F991B41C857009A9D16 /* CellType.swift in Sources */, - 21826AD11B3F535A00AA9641 /* SubtitleCell.swift in Sources */, - 36C8FE9B1B4EECF30004DA5B /* TableViewController.swift in Sources */, - 21826ACD1B3F51D000AA9641 /* Value1Cell.swift in Sources */, + 21F219671D10B7CF001EC0F5 /* Section.swift in Sources */, + 21F219621D10B7B9001EC0F5 /* Value2Cell.swift in Sources */, + 21F219601D10B784001EC0F5 /* CellType.swift in Sources */, + 21F219681D10B895001EC0F5 /* DataSource.swift in Sources */, + 21F219691D10B8E1001EC0F5 /* TableViewController.swift in Sources */, + 21F219641D10B7B9001EC0F5 /* ButtonCell.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Static/ButtonCell.swift b/Static/ButtonCell.swift index 316f116..00fc7b5 100644 --- a/Static/ButtonCell.swift +++ b/Static/ButtonCell.swift @@ -1,6 +1,6 @@ import UIKit -public class ButtonCell: UITableViewCell, CellType { +public class ButtonCell: UITableViewCell, Cell { // MARK: - Initializers diff --git a/Static/CellType.swift b/Static/CellType.swift index bf03d8d..d437b5e 100644 --- a/Static/CellType.swift +++ b/Static/CellType.swift @@ -1,19 +1,19 @@ import UIKit -public protocol CellType: class { +public protocol Cell: class { static func description() -> String static func nib() -> UINib? func configure(row: Row) } -extension CellType { +extension Cell { public static func nib() -> UINib? { return nil } } -extension CellType where Self: UITableViewCell { +extension Cell where Self: UITableViewCell { public func configure(row: Row) { textLabel?.text = row.text detailTextLabel?.text = row.detailText diff --git a/Static/DataSource.swift b/Static/DataSource.swift index 39e71eb..594d2c8 100644 --- a/Static/DataSource.swift +++ b/Static/DataSource.swift @@ -181,7 +181,7 @@ extension DataSource: UITableViewDataSource { if let row = rowForIndexPath(indexPath: indexPath) { let tableCell = tableView.dequeueReusableCell(withIdentifier: row.cellIdentifier, for: indexPath) - if let cell = tableCell as? CellType { + if let cell = tableCell as? Cell { cell.configure(row: row) } @@ -196,11 +196,11 @@ extension DataSource: UITableViewDataSource { } public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - return sectionForIndex(index: section)?.header?.title + return sectionForIndex(index: section)?.header?._title } public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { - return sectionForIndex(index: section)?.header?.view + return sectionForIndex(index: section)?.header?._view } public func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { @@ -208,11 +208,11 @@ extension DataSource: UITableViewDataSource { } public func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { - return sectionForIndex(index: section)?.footer?.title + return sectionForIndex(index: section)?.footer?._title } public func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { - return sectionForIndex(index: section)?.footer?.view + return sectionForIndex(index: section)?.footer?._view } public func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { diff --git a/Static/Row.swift b/Static/Row.swift index ddc356d..cf95a0a 100644 --- a/Static/Row.swift +++ b/Static/Row.swift @@ -106,7 +106,7 @@ public struct Row: Hashable, Equatable { public var selection: Selection? /// View to be used for the row. - public var cellClass: CellType.Type + public var cellClass: Cell.Type /// Additional information for the row. public var context: Context? @@ -134,7 +134,7 @@ public struct Row: Hashable, Equatable { // MARK: - Initializers public init(text: String? = nil, detailText: String? = nil, selection: Selection? = nil, - image: UIImage? = nil, accessory: Accessory = .none, cellClass: CellType.Type? = nil, context: Context? = nil, editActions: [EditAction] = [], uuid: String = UUID().uuidString) { + image: UIImage? = nil, accessory: Accessory = .none, cellClass: Cell.Type? = nil, context: Context? = nil, editActions: [EditAction] = [], uuid: String = UUID().uuidString) { self.uuid = uuid self.text = text diff --git a/Static/Section.swift b/Static/Section.swift index 8d10a5c..9d7e948 100644 --- a/Static/Section.swift +++ b/Static/Section.swift @@ -13,22 +13,22 @@ public struct Section: Hashable, Equatable { /// Custom view for the header or footer. The height will be the view's `bounds.height`. case view(UIView) - public var title: String? { + var _title: String? { switch self { case .title(let extremityTitle): return extremityTitle default: return nil } } - public var view: UIView? { + var _view: UIView? { switch self { case .view(let extremityView): return extremityView default: return nil } } - public var viewHeight: CGFloat? { - return view?.bounds.height + var viewHeight: CGFloat? { + return _view?.bounds.height } } diff --git a/Static/SubtitleCell.swift b/Static/SubtitleCell.swift index df15c78..eb6a86c 100644 --- a/Static/SubtitleCell.swift +++ b/Static/SubtitleCell.swift @@ -1,6 +1,6 @@ import UIKit -public class SubtitleCell: UITableViewCell, CellType { +public class SubtitleCell: UITableViewCell, Cell { public override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) } diff --git a/Static/Value1Cell.swift b/Static/Value1Cell.swift index e33116d..710d53c 100644 --- a/Static/Value1Cell.swift +++ b/Static/Value1Cell.swift @@ -1,6 +1,6 @@ import UIKit -public class Value1Cell: UITableViewCell, CellType { +public class Value1Cell: UITableViewCell, Cell { public override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: .value1, reuseIdentifier: reuseIdentifier) } diff --git a/Static/Value2Cell.swift b/Static/Value2Cell.swift index dd323ca..ad63cb6 100644 --- a/Static/Value2Cell.swift +++ b/Static/Value2Cell.swift @@ -1,6 +1,6 @@ import UIKit -public class Value2Cell: UITableViewCell, CellType { +public class Value2Cell: UITableViewCell, Cell { public override init(style: UITableViewCellStyle, reuseIdentifier: String?) { super.init(style: .value2, reuseIdentifier: reuseIdentifier) }