From 3d9a7af4b8151f194c2491cda6d421a680839407 Mon Sep 17 00:00:00 2001 From: eskizyen Date: Thu, 13 Oct 2016 16:04:03 +0300 Subject: [PATCH 1/5] Swift3 conversion --- Example/Example.xcodeproj/project.pbxproj | 11 + Example/Example/AppDelegate.swift | 22 +- Example/Example/CellTestViewController.swift | 24 +-- Example/Example/CustomCell.swift | 4 +- Example/Example/CustomCellModel.swift | 2 +- Example/Example/CustomHeaderView.swift | 2 +- .../HeaderFooterTestViewController.swift | 6 +- Example/Example/ViewController.swift | 8 +- Example/ExampleTests/ExampleTests.swift | 2 +- Hakuba/ArrayExt.swift | 48 ++--- Hakuba/BumpTracker.swift | 84 ++++---- Hakuba/Cell.swift | 26 +-- Hakuba/CellModel.swift | 48 ++--- Hakuba/Common.swift | 26 +-- Hakuba/Hakuba+Registration.swift | 12 +- Hakuba/Hakuba+UIScrollViewDelegate.swift | 22 +- Hakuba/Hakuba.swift | 188 +++++++++--------- Hakuba/HakubaDelegate.swift | 16 +- Hakuba/HeaderFooterView.swift | 12 +- Hakuba/HeaderFooterViewModel.swift | 28 +-- Hakuba/Label.swift | 4 +- Hakuba/RangeExt.swift | 6 +- Hakuba/Section.swift | 64 +++--- Hakuba/TableViewExt.swift | 22 +- 24 files changed, 347 insertions(+), 340 deletions(-) diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index f2b3a39..8d73ace 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -285,13 +285,16 @@ TargetAttributes = { 8C60BC5A1C89D4550006CE38 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; 8C60BC6E1C89D4550006CE38 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; TestTargetID = 8C60BC5A1C89D4550006CE38; }; 8C60BC791C89D4550006CE38 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; TestTargetID = 8C60BC5A1C89D4550006CE38; }; }; @@ -524,6 +527,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.nghialv.Example; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -536,6 +540,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.nghialv.Example; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -547,6 +552,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.nghialv.ExampleTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Example.app/Example"; }; name = Debug; @@ -559,6 +565,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.nghialv.ExampleTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Example.app/Example"; }; name = Release; @@ -567,9 +574,11 @@ isa = XCBuildConfiguration; buildSettings = { INFOPLIST_FILE = ExampleUITests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.nghialv.ExampleUITests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_TARGET_NAME = Example; USES_XCTRUNNER = YES; }; @@ -579,9 +588,11 @@ isa = XCBuildConfiguration; buildSettings = { INFOPLIST_FILE = ExampleUITests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.nghialv.ExampleUITests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_TARGET_NAME = Example; USES_XCTRUNNER = YES; }; diff --git a/Example/Example/AppDelegate.swift b/Example/Example/AppDelegate.swift index 2e40b31..91a985e 100644 --- a/Example/Example/AppDelegate.swift +++ b/Example/Example/AppDelegate.swift @@ -14,40 +14,36 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. return true } - func applicationWillResignActive(application: UIApplication) { + func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. } - func applicationDidEnterBackground(application: UIApplication) { + func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } - func applicationWillEnterForeground(application: UIApplication) { + func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. } - func applicationDidBecomeActive(application: UIApplication) { + func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } - func applicationWillTerminate(application: UIApplication) { + func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } } -func delay(delay:Double, closure:()->()) { - dispatch_after( - dispatch_time( - DISPATCH_TIME_NOW, - Int64(delay * Double(NSEC_PER_SEC)) - ), - dispatch_get_main_queue(), closure) +func delay(_ delay:Double, closure:@escaping ()->()) { + DispatchQueue.main.asyncAfter( + deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure) } diff --git a/Example/Example/CellTestViewController.swift b/Example/Example/CellTestViewController.swift index 49fce8e..74874d5 100644 --- a/Example/Example/CellTestViewController.swift +++ b/Example/Example/CellTestViewController.swift @@ -9,17 +9,17 @@ import UIKit enum SectionIndex: Int, SectionIndexType { - case Top - case Center + case top + case center static let count = 2 } class CellTestViewController: UIViewController { @IBOutlet weak var tableView: UITableView! - private lazy var hakuba: Hakuba = Hakuba(tableView: self.tableView) + fileprivate lazy var hakuba: Hakuba = Hakuba(tableView: self.tableView) - override func viewWillAppear(animated: Bool) { + override func viewWillAppear(_ animated: Bool) { hakuba.deselectAllCells(animated: true) } @@ -42,8 +42,8 @@ class CellTestViewController: UIViewController { .reset(SectionIndex) .bump() - let topSection = hakuba[SectionIndex.Top] - let centerSection = hakuba[SectionIndex.Center] + let topSection = hakuba[SectionIndex.top] + let centerSection = hakuba[SectionIndex.center] topSection .reset(topCellmodels) @@ -67,13 +67,13 @@ class CellTestViewController: UIViewController { delay(1.5) { centerSection .append(centerCellmodels) - .bump(.Left) + .bump(.left) } delay(3) { centerSection .remove(2...4) - .bump(.Right) + .bump(.right) } delay(5) { @@ -85,19 +85,19 @@ class CellTestViewController: UIViewController { delay(7.5) { topSection .remove(1) - .bump(.Middle) + .bump(.middle) } delay(10) { topSection .remove(0) - .bump(.Right) + .bump(.right) } delay(12.5) { topSection .remove(0) - .bump(.Right) + .bump(.right) } delay(15) { @@ -109,7 +109,7 @@ class CellTestViewController: UIViewController { func pushChildViewController() { let storyboard = UIStoryboard(name: "Main", bundle: nil) - let vc = storyboard.instantiateViewControllerWithIdentifier("ChildViewController") as! ChildViewController + let vc = storyboard.instantiateViewController(withIdentifier: "ChildViewController") as! ChildViewController self.navigationController?.pushViewController(vc, animated: true) } } diff --git a/Example/Example/CustomCell.swift b/Example/Example/CustomCell.swift index 086b5bd..673cecc 100644 --- a/Example/Example/CustomCell.swift +++ b/Example/Example/CustomCell.swift @@ -18,10 +18,10 @@ class CustomCell: Cell, CellType { return } - titleLabel.text = cellmodel.title + "(\(cellmodel.indexPath.section),\(cellmodel.indexPath.row))" + titleLabel.text = cellmodel.title + "(\((cellmodel.indexPath as NSIndexPath).section),\((cellmodel.indexPath as NSIndexPath).row))" } - override func willDisplay(tableView: UITableView) { + override func willDisplay(_ tableView: UITableView) { super.willDisplay(tableView) } diff --git a/Example/Example/CustomCellModel.swift b/Example/Example/CustomCellModel.swift index d8de15b..0a4b677 100644 --- a/Example/Example/CustomCellModel.swift +++ b/Example/Example/CustomCellModel.swift @@ -11,7 +11,7 @@ import Foundation class CustomCellModel: CellModel { let title: String - init(title: String, selectionHandler: SelectionHandler) { + init(title: String, selectionHandler: @escaping SelectionHandler) { self.title = title super.init(cell: CustomCell.self, selectionHandler: selectionHandler) } diff --git a/Example/Example/CustomHeaderView.swift b/Example/Example/CustomHeaderView.swift index f36a224..8f7179a 100644 --- a/Example/Example/CustomHeaderView.swift +++ b/Example/Example/CustomHeaderView.swift @@ -22,7 +22,7 @@ final class CustomHeaderView: HeaderFooterView, HeaderFooterViewType { label.text = vm.text } - override func didChangeFloatingState(isFloating: Bool, section: Int) { + override func didChangeFloatingState(_ isFloating: Bool, section: Int) { super.didChangeFloatingState(isFloating, section: section) let title = isFloating ? "F \(section)" : "n \(section)" diff --git a/Example/Example/HeaderFooterTestViewController.swift b/Example/Example/HeaderFooterTestViewController.swift index 48dc586..369fff8 100644 --- a/Example/Example/HeaderFooterTestViewController.swift +++ b/Example/Example/HeaderFooterTestViewController.swift @@ -10,9 +10,9 @@ import UIKit class HeaderFooterTestViewController: UIViewController { @IBOutlet weak var tableView: UITableView! - private lazy var hakuba: Hakuba = Hakuba(tableView: self.tableView) + fileprivate lazy var hakuba: Hakuba = Hakuba(tableView: self.tableView) - override func viewWillAppear(animated: Bool) { + override func viewWillAppear(_ animated: Bool) { hakuba.deselectAllCells(animated: true) } @@ -65,7 +65,7 @@ class HeaderFooterTestViewController: UIViewController { func pushChildViewController() { let storyboard = UIStoryboard(name: "Main", bundle: nil) - let vc = storyboard.instantiateViewControllerWithIdentifier("ChildViewController") as! ChildViewController + let vc = storyboard.instantiateViewController(withIdentifier: "ChildViewController") as! ChildViewController self.navigationController?.pushViewController(vc, animated: true) } } diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index 03965df..fdc116c 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -10,9 +10,9 @@ import UIKit class ViewController: UIViewController { @IBOutlet weak var tableView: UITableView! - private lazy var hakuba: Hakuba = Hakuba(tableView: self.tableView) + fileprivate lazy var hakuba: Hakuba = Hakuba(tableView: self.tableView) - override func viewWillAppear(animated: Bool) { + override func viewWillAppear(_ animated: Bool) { hakuba.deselectAllCells(animated: true) } @@ -22,14 +22,14 @@ class ViewController: UIViewController { let cm1 = CustomCellModel(title: "Test cell") { [weak self] _ in let storyboard = UIStoryboard(name: "Main", bundle: nil) - let vc = storyboard.instantiateViewControllerWithIdentifier("CellTestViewController") as! CellTestViewController + let vc = storyboard.instantiateViewController(withIdentifier: "CellTestViewController") as! CellTestViewController self?.navigationController?.pushViewController(vc, animated: true) } let cm2 = CustomCellModel(title: "Test header footer") { [weak self] _ in let storyboard = UIStoryboard(name: "Main", bundle: nil) - let vc = storyboard.instantiateViewControllerWithIdentifier("HeaderFooterTestViewController") as! HeaderFooterTestViewController + let vc = storyboard.instantiateViewController(withIdentifier: "HeaderFooterTestViewController") as! HeaderFooterTestViewController self?.navigationController?.pushViewController(vc, animated: true) } diff --git a/Example/ExampleTests/ExampleTests.swift b/Example/ExampleTests/ExampleTests.swift index 86f0af3..504a62a 100644 --- a/Example/ExampleTests/ExampleTests.swift +++ b/Example/ExampleTests/ExampleTests.swift @@ -28,7 +28,7 @@ class ExampleTests: XCTestCase { func testPerformanceExample() { // This is an example of a performance test case. - self.measureBlock { + self.measure { // Put the code you want to measure the time of here. } } diff --git a/Hakuba/ArrayExt.swift b/Hakuba/ArrayExt.swift index 9018510..f4ac2b6 100644 --- a/Hakuba/ArrayExt.swift +++ b/Hakuba/ArrayExt.swift @@ -13,34 +13,34 @@ extension Array { return !isEmpty } - func hasIndex(index: Int) -> Bool { + func hasIndex(_ index: Int) -> Bool { return indices ~= index } - func getSafeIndex(index: Int) -> Int { - let mIndex = max(0, index) - return min(count, mIndex) + func getSafeIndex(_ index: Int) -> Int { + let mIndex = Swift.max(0, index) + return Swift.min(count, mIndex) } - func getSafeRange(range: Range) -> Range? { - let start = max(0, range.startIndex) - let end = min(count, range.endIndex) + func getSafeRange(_ range: Range) -> CountableRange? { + let start = Swift.max(0, range.lowerBound) + let end = Swift.min(count, range.upperBound) return start <= end ? start.. Element? { + func get(_ index: Int) -> Element? { return hasIndex(index) ? self[index] : nil } - mutating func append(newArray: Array) -> Range { + mutating func append(_ newArray: Array) -> CountableRange { let range = count..<(count + newArray.count) self += newArray return range } - mutating func insert(newArray: Array, atIndex index: Int) -> Range { - let mIndex = max(0, index) - let start = min(count, mIndex) + mutating func insert(_ newArray: Array, atIndex index: Int) -> CountableRange { + let mIndex = Swift.max(0, index) + let start = Swift.min(count, mIndex) let end = start + newArray.count let left = self[0.. Range? { + mutating func remove(_ index: Int) -> CountableRange? { if !hasIndex(index) { return nil } - removeAtIndex(index) + self.remove(at: index) return index..<(index + 1) } - mutating func remove(range: Range) -> Range? { + mutating func remove(_ range: Range) -> CountableRange? { if let sr = getSafeRange(range) { - removeRange(sr) + removeSubrange(sr) return sr } return nil } - mutating func remove (element: T) { + mutating func remove (_ element: T) { let anotherSelf = self - removeAll(keepCapacity: true) + removeAll(keepingCapacity: true) anotherSelf.each { (index: Int, current: Element) in if (current as! T) !== element { @@ -90,13 +90,13 @@ extension Array { } } - mutating func removeLast() -> Range? { + mutating func removeLast() -> CountableRange? { return remove(count - 1) } - func each(exe: (Int, Element) -> ()) { - for (index, item) in enumerate() { + func each(_ exe: (Int, Element) -> ()) { + for (index, item) in enumerated() { exe(index, item) } } -} \ No newline at end of file +} diff --git a/Hakuba/BumpTracker.swift b/Hakuba/BumpTracker.swift index bd0167f..80231f9 100644 --- a/Hakuba/BumpTracker.swift +++ b/Hakuba/BumpTracker.swift @@ -9,102 +9,102 @@ import Foundation private enum UpdateState { - case Begin - case Reload - case Insert([Int]) - case Move(Int, Int) - case Remove([Int]) + case begin + case reload + case insert([Int]) + case move(Int, Int) + case remove([Int]) } final class BumpTracker { - private var state: UpdateState = .Begin + fileprivate var state: UpdateState = .begin var changed: Bool { - if case .Begin = state { + if case .begin = state { return false } return true } func didBump() { - state = .Begin + state = .begin } func didReset() { - state = .Reload + state = .reload } - func didInsert(indexes: [Int]) { + func didInsert(_ indexes: [Int]) { switch state { - case .Begin: - state = .Insert(indexes) + case .begin: + state = .insert(indexes) default: - state = .Reload + state = .reload } } - func didMove(from: Int, to: Int) { + func didMove(_ from: Int, to: Int) { switch state { - case .Begin: - state = .Move(from, to) + case .begin: + state = .move(from, to) default: - state = .Reload + state = .reload } } - func didRemove(indexes: [Int]) { + func didRemove(_ indexes: [Int]) { switch state { - case .Begin: - state = .Remove(indexes) + case .begin: + state = .remove(indexes) default: - state = .Reload + state = .reload } } - func getSectionBumpType(index: Int) -> SectionBumpType { - let toIndexPath = { (row: Int) -> NSIndexPath in - return NSIndexPath(forRow: row, inSection: index) + func getSectionBumpType(_ index: Int) -> SectionBumpType { + let toIndexPath = { (row: Int) -> IndexPath in + return IndexPath(row: row, section: index) } switch state { - case .Insert(let indexes) where indexes.isNotEmpty: - return .Insert(indexes.map(toIndexPath)) + case .insert(let indexes) where indexes.isNotEmpty: + return .insert(indexes.map(toIndexPath)) - case .Move(let from, let to): - return .Move(toIndexPath(from), toIndexPath(to)) + case .move(let from, let to): + return .move(toIndexPath(from), toIndexPath(to)) - case .Remove(let indexes) where indexes.isNotEmpty: - return .Delete(indexes.map(toIndexPath)) + case .remove(let indexes) where indexes.isNotEmpty: + return .delete(indexes.map(toIndexPath)) default: - return .Reload(NSIndexSet(index: index)) + return .reload(IndexSet(integer: index)) } } func getHakubaBumpType() -> HakubaBumpType { - let toIndexSet = { (indexes: [Int]) -> NSIndexSet in + let toIndexSet = { (indexes: [Int]) -> IndexSet in let indexSet = NSMutableIndexSet() for index in indexes { - indexSet.addIndex(index) + indexSet.add(index) } - return indexSet + return indexSet as IndexSet } switch state { - case .Insert(let indexes) where indexes.isNotEmpty: - return .Insert(toIndexSet(indexes)) + case .insert(let indexes) where indexes.isNotEmpty: + return .insert(toIndexSet(indexes)) - case .Move(let from, let to): - return .Move(from, to) + case .move(let from, let to): + return .move(from, to) - case .Remove(let indexes) where indexes.isNotEmpty: - return .Delete(toIndexSet(indexes)) + case .remove(let indexes) where indexes.isNotEmpty: + return .delete(toIndexSet(indexes)) default: - return .Reload + return .reload } } -} \ No newline at end of file +} diff --git a/Hakuba/Cell.swift b/Hakuba/Cell.swift index accb9ed..a6e3585 100644 --- a/Hakuba/Cell.swift +++ b/Hakuba/Cell.swift @@ -8,50 +8,50 @@ import UIKit -public class Cell: UITableViewCell { +open class Cell: UITableViewCell { weak var _cellmodel: CellModel? - func configureCell(cellmodel: CellModel) { + func configureCell(_ cellmodel: CellModel) { _cellmodel = cellmodel configure() } - public func configure() { + open func configure() { } } // MARK - Cell events public extension Cell { - func willDisplay(tableView: UITableView) { + func willDisplay(_ tableView: UITableView) { } - func didEndDisplay(tableView: UITableView) { + func didEndDisplay(_ tableView: UITableView) { } - func willSelect(tableView: UITableView, indexPath: NSIndexPath) -> NSIndexPath? { + func willSelect(_ tableView: UITableView, indexPath: IndexPath) -> IndexPath? { return indexPath } - func didSelect(tableView: UITableView) { + func didSelect(_ tableView: UITableView) { } - func willDeselect(tableView: UITableView, indexPath: NSIndexPath) -> NSIndexPath? { + func willDeselect(_ tableView: UITableView, indexPath: IndexPath) -> IndexPath? { return indexPath } - func didDeselect(tableView: UITableView) { + func didDeselect(_ tableView: UITableView) { } - func willBeginEditing(tableView: UITableView) { + func willBeginEditing(_ tableView: UITableView) { } - func didEndEditing(tableView: UITableView) { + func didEndEditing(_ tableView: UITableView) { } - func didHighlight(tableView: UITableView) { + func didHighlight(_ tableView: UITableView) { } - func didUnhighlight(tableView: UITableView) { + func didUnhighlight(_ tableView: UITableView) { } } diff --git a/Hakuba/CellModel.swift b/Hakuba/CellModel.swift index 30717f8..7667050 100644 --- a/Hakuba/CellModel.swift +++ b/Hakuba/CellModel.swift @@ -9,24 +9,24 @@ import UIKit protocol CellModelDelegate: class { - func bumpMe(type: ItemBumpType, animation: Animation) - func getOffscreenCell(identifier: String) -> Cell + func bumpMe(_ type: ItemBumpType, animation: Animation) + func getOffscreenCell(_ identifier: String) -> Cell func tableViewWidth() -> CGFloat - func deselectCell(indexPath: NSIndexPath, animated: Bool) + func deselectCell(_ indexPath: IndexPath, animated: Bool) } -public class CellModel { +open class CellModel { weak var delegate: CellModelDelegate? - public let reuseIdentifier: String - public internal(set) var indexPath = NSIndexPath(forRow: 0, inSection: 0) - public var selectionHandler: SelectionHandler? + open let reuseIdentifier: String + open internal(set) var indexPath = IndexPath(row: 0, section: 0) + open var selectionHandler: SelectionHandler? - public var editable = false - public var editingStyle: UITableViewCellEditingStyle = .None - public var shouldHighlight = true + open var editable = false + open var editingStyle: UITableViewCellEditingStyle = .none + open var shouldHighlight = true - public var height: CGFloat { + open var height: CGFloat { get { return dynamicHeightEnabled ? calculateHeight() : estimatedHeight } @@ -35,28 +35,28 @@ public class CellModel { } } - public var dynamicHeightEnabled: Bool = false { + open var dynamicHeightEnabled: Bool = false { didSet { calculatedHeight = nil } } - private var estimatedHeight: CGFloat = 0 - private var calculatedHeight: CGFloat? + fileprivate var estimatedHeight: CGFloat = 0 + fileprivate var calculatedHeight: CGFloat? - public init(cell: T.Type, height: CGFloat = 44, selectionHandler: SelectionHandler? = nil) { + public init(cell: T.Type, height: CGFloat = 44, selectionHandler: SelectionHandler? = nil) where T: Cell, T: CellType { self.reuseIdentifier = cell.reuseIdentifier self.estimatedHeight = height self.selectionHandler = selectionHandler } - public func bump(animation: Animation = .None) -> Self { + open func bump(_ animation: Animation = .none) -> Self { calculatedHeight = nil - delegate?.bumpMe(ItemBumpType.Reload(indexPath), animation: animation) + delegate?.bumpMe(ItemBumpType.reload(indexPath), animation: animation) return self } - public func deselect(animated: Bool) { + open func deselect(_ animated: Bool) { delegate?.deselectCell(indexPath, animated: animated) } } @@ -64,12 +64,12 @@ public class CellModel { // MARK - Internal methods extension CellModel { - func setup(indexPath: NSIndexPath, delegate: CellModelDelegate) { + func setup(_ indexPath: IndexPath, delegate: CellModelDelegate) { self.indexPath = indexPath self.delegate = delegate } - func didSelect(cell: Cell) { + func didSelect(_ cell: Cell) { selectionHandler?(cell) } } @@ -88,15 +88,15 @@ private extension CellModel { cell.configureCell(self) - let width = delegate?.tableViewWidth() ?? UIScreen.mainScreen().bounds.width - cell.bounds = CGRectMake(0, 0, width, cell.bounds.height) + let width = delegate?.tableViewWidth() ?? UIScreen.main.bounds.width + cell.bounds = CGRect(x: 0, y: 0, width: width, height: cell.bounds.height) cell.setNeedsLayout() cell.layoutIfNeeded() - let size = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize) + let size = cell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize) let height = size.height + 1 calculatedHeight = height return height } -} \ No newline at end of file +} diff --git a/Hakuba/Common.swift b/Hakuba/Common.swift index fd7ff3a..181a654 100644 --- a/Hakuba/Common.swift +++ b/Hakuba/Common.swift @@ -24,25 +24,25 @@ public extension SectionIndexType where Self: RawRepresentable, Self.RawValue == } public enum HakubaBumpType { - case Reload - case Insert(NSIndexSet) - case Move(Int, Int) - case Delete(NSIndexSet) + case reload + case insert(IndexSet) + case move(Int, Int) + case delete(IndexSet) } public enum SectionBumpType { - case Reload(NSIndexSet) - case Insert([NSIndexPath]) - case Move(NSIndexPath, NSIndexPath) - case Delete([NSIndexPath]) + case reload(IndexSet) + case insert([IndexPath]) + case move(IndexPath, IndexPath) + case delete([IndexPath]) } public enum ItemBumpType { - case Reload(NSIndexPath) - case ReloadHeader(Int) - case ReloadFooter(Int) + case reload(IndexPath) + case reloadHeader(Int) + case reloadFooter(Int) } -func classNameOf(aClass: AnyClass) -> String { - return NSStringFromClass(aClass).componentsSeparatedByString(".").last! +func classNameOf(_ aClass: AnyClass) -> String { + return NSStringFromClass(aClass).components(separatedBy: ".").last! } diff --git a/Hakuba/Hakuba+Registration.swift b/Hakuba/Hakuba+Registration.swift index e3c12b3..c864d2e 100644 --- a/Hakuba/Hakuba+Registration.swift +++ b/Hakuba/Hakuba+Registration.swift @@ -9,36 +9,36 @@ import UIKit public extension Hakuba { - func registerCellByNib(cellType: T.Type) -> Self { + func registerCellByNib(_ cellType: T.Type) -> Self where T: Cell, T: CellType { tableView?.registerCellByNib(cellType) return self } - func registerCellsByNib(cellTypes: T.Type...) -> Self { + func registerCellsByNib(_ cellTypes: T.Type...) -> Self where T: Cell, T: CellType { for cellType in cellTypes { registerCellByNib(cellType) } return self } - func registerCell(cellType: T.Type) -> Self { + func registerCell(_ cellType: T.Type) -> Self { tableView?.registerCell(cellType) return self } - func registerCells(cellTypes: T.Type...) -> Self { + func registerCells(_ cellTypes: T.Type...) -> Self { for cellType in cellTypes { registerCell(cellType) } return self } - func registerHeaderFooterByNib(t: T.Type) -> Self { + func registerHeaderFooterByNib(_ t: T.Type) -> Self where T: HeaderFooterView, T: HeaderFooterViewType { tableView?.registerHeaderFooterByNib(t) return self } - func registerHeaderFooter(t: T.Type) -> Self { + func registerHeaderFooter(_ t: T.Type) -> Self where T: HeaderFooterView, T: HeaderFooterViewType { tableView?.registerHeaderFooter(t) return self } diff --git a/Hakuba/Hakuba+UIScrollViewDelegate.swift b/Hakuba/Hakuba+UIScrollViewDelegate.swift index 746011f..1a4a3c4 100644 --- a/Hakuba/Hakuba+UIScrollViewDelegate.swift +++ b/Hakuba/Hakuba+UIScrollViewDelegate.swift @@ -10,17 +10,17 @@ import UIKit extension Hakuba { - public func scrollViewDidScroll(scrollView: UIScrollView) { + public func scrollViewDidScroll(_ scrollView: UIScrollView) { delegate?.scrollViewDidScroll?(scrollView) if let indexPath = tableView?.indexPathsForVisibleRows?.first { - let topSection = indexPath.section + let topSection = (indexPath as NSIndexPath).section if currentTopSection != topSection { - if let headerView = tableView?.headerViewForSection(currentTopSection) as? HeaderFooterView { + if let headerView = tableView?.headerView(forSection: currentTopSection) as? HeaderFooterView { headerView.didChangeFloatingState(false, section: currentTopSection) } - if let headerView = tableView?.headerViewForSection(topSection) as? HeaderFooterView { + if let headerView = tableView?.headerView(forSection: topSection) as? HeaderFooterView { headerView.didChangeFloatingState(true, section: topSection) } if currentTopSection > topSection { @@ -44,31 +44,31 @@ extension Hakuba { } } - public func scrollViewWillBeginDecelerating(scrollView: UIScrollView) { + public func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) { delegate?.scrollViewWillBeginDecelerating?(scrollView) } - public func scrollViewDidEndDecelerating(scrollView: UIScrollView) { + public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { delegate?.scrollViewDidEndDecelerating?(scrollView) } - public func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) { + public func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) { delegate?.scrollViewDidEndScrollingAnimation?(scrollView) } - public func scrollViewDidScrollToTop(scrollView: UIScrollView) { + public func scrollViewDidScrollToTop(_ scrollView: UIScrollView) { delegate?.scrollViewDidScrollToTop?(scrollView) } - public func scrollViewWillBeginDragging(scrollView: UIScrollView) { + public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { delegate?.scrollViewWillBeginDragging?(scrollView) } - public func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) { + public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) { delegate?.scrollViewWillEndDragging?(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset) } - public func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) { + public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { delegate?.scrollViewDidEndDragging?(scrollView, willDecelerate: decelerate) } } diff --git a/Hakuba/Hakuba.swift b/Hakuba/Hakuba.swift index 3583e17..6e17db9 100644 --- a/Hakuba/Hakuba.swift +++ b/Hakuba/Hakuba.swift @@ -11,20 +11,20 @@ import UIKit final public class Hakuba: NSObject { weak var tableView: UITableView? public weak var delegate: HakubaDelegate? - public private(set) var sections: [Section] = [] + public fileprivate(set) var sections: [Section] = [] public var loadmoreHandler: (() -> ())? public var loadmoreEnabled = false public var loadmoreThreshold: CGFloat = 25 - private let bumpTracker = BumpTracker() - private var offscreenCells: [String: Cell] = [:] + fileprivate let bumpTracker = BumpTracker() + fileprivate var offscreenCells: [String: Cell] = [:] - public var selectedRows: [NSIndexPath] { + public var selectedRows: [IndexPath] { return tableView?.indexPathsForSelectedRows ?? [] } - public var visibleRows: [NSIndexPath] { + public var visibleRows: [IndexPath] { return tableView?.indexPathsForVisibleRows ?? [] } @@ -40,7 +40,7 @@ final public class Hakuba: NSObject { } public var cellEditable = false - public var commitEditingHandler: ((UITableViewCellEditingStyle, NSIndexPath) -> ())? + public var commitEditingHandler: ((UITableViewCellEditingStyle, IndexPath) -> ())? public subscript(index: SectionIndexType) -> Section { get { @@ -61,19 +61,19 @@ final public class Hakuba: NSObject { } } - subscript(indexPath: NSIndexPath) -> CellModel? { - return self[indexPath.section][indexPath.row] + subscript(indexPath: IndexPath) -> CellModel? { + return self[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row] } - public func getCellmodel(indexPath: NSIndexPath) -> CellModel? { - return sections.get(indexPath.section)?[indexPath.row] + public func getCellmodel(_ indexPath: IndexPath) -> CellModel? { + return sections.get((indexPath as NSIndexPath).section)?[(indexPath as NSIndexPath).row] } - public func getSection(index: Int) -> Section? { + public func getSection(_ index: Int) -> Section? { return sections.get(index) } - public func getSection(index: SectionIndexType) -> Section? { + public func getSection(_ index: SectionIndexType) -> Section? { return getSection(index.intValue) } @@ -89,21 +89,21 @@ final public class Hakuba: NSObject { tableView?.dataSource = nil } - public func bump(animation: Animation = .None) -> Self { + public func bump(_ animation: Animation = .none) -> Self { let changedCount = sections.reduce(0) { $0 + ($1.changed ? 1 : 0) } if changedCount == 0 { switch bumpTracker.getHakubaBumpType() { - case .Reload: + case .reload: tableView?.reloadData() - case let .Insert(indexSet): - tableView?.insertSections(indexSet, withRowAnimation: animation) + case let .insert(indexSet): + tableView?.insertSections(indexSet, with: animation) - case let .Delete(indexSet): - tableView?.deleteSections(indexSet, withRowAnimation: animation) + case let .delete(indexSet): + tableView?.deleteSections(indexSet, with: animation) - case let .Move(from, to): + case let .move(from, to): tableView?.moveSection(from, toSection: to) } } else { @@ -120,26 +120,26 @@ final public class Hakuba: NSObject { // MARK - UITableView methods public extension Hakuba { - func setEditing(editing: Bool, animated: Bool) { + func setEditing(_ editing: Bool, animated: Bool) { tableView?.setEditing(editing, animated: animated) } - func selectCell(indexPath: NSIndexPath, animated: Bool, scrollPosition: UITableViewScrollPosition) { - tableView?.selectRowAtIndexPath(indexPath, animated: animated, scrollPosition: scrollPosition) + func selectCell(_ indexPath: IndexPath, animated: Bool, scrollPosition: UITableViewScrollPosition) { + tableView?.selectRow(at: indexPath, animated: animated, scrollPosition: scrollPosition) } - func deselectCell(indexPath: NSIndexPath, animated: Bool) { - tableView?.deselectRowAtIndexPath(indexPath, animated: animated) + func deselectCell(_ indexPath: IndexPath, animated: Bool) { + tableView?.deselectRow(at: indexPath, animated: animated) } - func deselectAllCells(animated animated: Bool) { + func deselectAllCells(animated: Bool) { selectedRows.forEach { - tableView?.deselectRowAtIndexPath($0, animated: animated) + tableView?.deselectRow(at: $0, animated: animated) } } - func getCell(indexPath: NSIndexPath) -> Cell? { - return tableView?.cellForRowAtIndexPath(indexPath) as? Cell + func getCell(_ indexPath: IndexPath) -> Cell? { + return tableView?.cellForRow(at: indexPath) as? Cell } } @@ -148,7 +148,7 @@ public extension Hakuba { public extension Hakuba { // MARK - Reset - func reset(listType: SectionIndexType.Type) -> Self { + func reset(_ listType: SectionIndexType.Type) -> Self { let sections = (0.. Self { + func reset(_ section: Section) -> Self { return reset([section]) } - func reset(sections: [Section]) -> Self { + func reset(_ sections: [Section]) -> Self { setupSections(sections, fromIndex: 0) self.sections = sections bumpTracker.didReset() @@ -170,21 +170,21 @@ public extension Hakuba { // MARK - Append - func append(section: Section) -> Self { + func append(_ section: Section) -> Self { return append([section]) } - func append(sections: [Section]) -> Self { + func append(_ sections: [Section]) -> Self { return insert(sections, atIndex: sectionsCount) } // MARK - Insert - func insert(section: Section, atIndex index: Int) -> Self { + func insert(_ section: Section, atIndex index: Int) -> Self { return insert([section], atIndex: index) } - func insert(sections: [Section], atIndex index: Int) -> Self { + func insert(_ sections: [Section], atIndex index: Int) -> Self { guard sections.isNotEmpty else { return self } @@ -197,40 +197,40 @@ public extension Hakuba { return self } - func insertBeforeLast(section: Section) -> Self { + func insertBeforeLast(_ section: Section) -> Self { return insertBeforeLast([section]) } - func insertBeforeLast(sections: [Section]) -> Self { + func insertBeforeLast(_ sections: [Section]) -> Self { let index = max(sections.count - 1, 0) return insert(sections, atIndex: index) } // MARK - Remove - func remove(index: Int) -> Self { + func remove(_ index: Int) -> Self { return remove(indexes: [index]) } - func remove(range: Range) -> Self { + func remove(_ range: CountableClosedRange) -> Self { let indexes = range.map { $0 } return remove(indexes: indexes) } - func remove(indexes indexes: [Int]) -> Self { + func remove(indexes: [Int]) -> Self { guard indexes.isNotEmpty else { return self } let sortedIndexes = indexes - .sort(<) + .sorted(by: <) .filter { $0 >= 0 && $0 < self.sectionsCount } var remainSections: [Section] = [] var i = 0 for j in 0.. Self { + func remove(_ section: Section) -> Self { let index = section.index guard index >= 0 && index < sectionsCount else { @@ -271,7 +271,7 @@ public extension Hakuba { // MAKR - Move - func move(from: Int, to: Int) -> Self { + func move(_ from: Int, to: Int) -> Self { sections.move(fromIndex: from, toIndex: to) setupSections([sections[from]], fromIndex: from) setupSections([sections[to]], fromIndex: to) @@ -284,41 +284,41 @@ public extension Hakuba { // MARK - SectionDelegate, CellModelDelegate extension Hakuba: SectionDelegate, CellModelDelegate { - func bumpMe(type: SectionBumpType, animation: Animation) { + func bumpMe(_ type: SectionBumpType, animation: Animation) { switch type { - case .Reload(let indexSet): - tableView?.reloadSections(indexSet, withRowAnimation: animation) + case .reload(let indexSet): + tableView?.reloadSections(indexSet, with: animation) - case .Insert(let indexPaths): - tableView?.insertRowsAtIndexPaths(indexPaths, withRowAnimation: animation) + case .insert(let indexPaths): + tableView?.insertRows(at: indexPaths, with: animation) - case .Move(let ori, let des): - tableView?.moveRowAtIndexPath(ori, toIndexPath: des) + case .move(let ori, let des): + tableView?.moveRow(at: ori, to: des) - case .Delete(let indexPaths): - tableView?.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: animation) + case .delete(let indexPaths): + tableView?.deleteRows(at: indexPaths, with: animation) } } - func bumpMe(type: ItemBumpType, animation: Animation) { + func bumpMe(_ type: ItemBumpType, animation: Animation) { switch type { - case .Reload(let indexPath): - tableView?.reloadRowsAtIndexPaths([indexPath], withRowAnimation: animation) + case .reload(let indexPath): + tableView?.reloadRows(at: [indexPath], with: animation) - case .ReloadHeader: + case .reloadHeader: break - case .ReloadFooter: + case .reloadFooter: break } } - func getOffscreenCell(identifier: String) -> Cell { + func getOffscreenCell(_ identifier: String) -> Cell { if let cell = offscreenCells[identifier] { return cell } - if let cell = tableView?.dequeueReusableCellWithIdentifier(identifier) as? Cell { + if let cell = tableView?.dequeueReusableCell(withIdentifier: identifier) as? Cell { offscreenCells[identifier] = cell return cell } @@ -333,19 +333,19 @@ extension Hakuba: SectionDelegate, CellModelDelegate { // MARK - UITableViewDelegate cell extension Hakuba: UITableViewDelegate { - public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { + public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return getCellmodel(indexPath)?.height ?? 0 } // public func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { // } - public func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { + public func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? { return getCell(indexPath)?.willSelect(tableView, indexPath: indexPath) } - public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - guard let cellmodel = getCellmodel(indexPath), cell = getCell(indexPath) else { + public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + guard let cellmodel = getCellmodel(indexPath), let cell = getCell(indexPath) else { return } @@ -353,35 +353,35 @@ extension Hakuba: UITableViewDelegate { cell.didSelect(tableView) } - public func tableView(tableView: UITableView, willDeselectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { + public func tableView(_ tableView: UITableView, willDeselectRowAt indexPath: IndexPath) -> IndexPath? { return getCell(indexPath)?.willDeselect(tableView, indexPath: indexPath) } - public func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { + public func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { getCell(indexPath)?.didDeselect(tableView) } - public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { + public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { (cell as? Cell)?.willDisplay(tableView) } - public func tableView(tableView: UITableView, didEndDisplayingCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { + public func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) { (cell as? Cell)?.didEndDisplay(tableView) } - public func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle { - return getCellmodel(indexPath)?.editingStyle ?? .None + public func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { + return getCellmodel(indexPath)?.editingStyle ?? .none } - public func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool { + public func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool { return getCellmodel(indexPath)?.shouldHighlight ?? true } - public func tableView(tableView: UITableView, didHighlightRowAtIndexPath indexPath: NSIndexPath) { + public func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) { getCell(indexPath)?.didHighlight(tableView) } - public func tableView(tableView: UITableView, didUnhighlightRowAtIndexPath indexPath: NSIndexPath) { + public func tableView(_ tableView: UITableView, didUnhighlightRowAt indexPath: IndexPath) { getCell(indexPath)?.didUnhighlight(tableView) } } @@ -389,38 +389,38 @@ extension Hakuba: UITableViewDelegate { // MARK - UITableViewDelegate header-footer extension Hakuba { - public func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { + public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return sections.get(section)?.header?.height ?? 0 } - public func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { + public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { guard let header = sections.get(section)?.header else { return nil } - let headerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(header.reuseIdentifier) as? HeaderFooterView + let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: header.reuseIdentifier) as? HeaderFooterView headerView?.configureView(header) return headerView } - public func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { + public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat { return sections.get(section)?.footer?.height ?? 0 } - public func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { + public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { guard let footer = sections.get(section)?.footer else { return nil } - let footerView = tableView.dequeueReusableHeaderFooterViewWithIdentifier(footer.reuseIdentifier) as? HeaderFooterView + let footerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: footer.reuseIdentifier) as? HeaderFooterView footerView?.configureView(footer) return footerView } - public func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { - guard let view = view as? HeaderFooterView where section == willFloatingSection else { + public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { + guard let view = view as? HeaderFooterView , section == willFloatingSection else { return } @@ -429,7 +429,7 @@ extension Hakuba { willFloatingSection = NSNotFound } - public func tableView(tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { + public func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) { guard let view = view as? HeaderFooterView else { return } @@ -437,7 +437,7 @@ extension Hakuba { view.willDisplay(tableView, section: section) } - public func tableView(tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) { + public func tableView(_ tableView: UITableView, didEndDisplayingHeaderView view: UIView, forSection section: Int) { guard let view = view as? HeaderFooterView else { return } @@ -445,7 +445,7 @@ extension Hakuba { view.didEndDisplaying(tableView, section: section) } - public func tableView(tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int) { + public func tableView(_ tableView: UITableView, didEndDisplayingFooterView view: UIView, forSection section: Int) { guard let view = view as? HeaderFooterView else { return } @@ -457,17 +457,17 @@ extension Hakuba { // MARK - UITableViewDataSource extension Hakuba: UITableViewDataSource { - public func numberOfSectionsInTableView(tableView: UITableView) -> Int { + public func numberOfSections(in tableView: UITableView) -> Int { return sectionsCount } - public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return sections.get(section)?.count ?? 0 } - public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cellmodel = getCellmodel(indexPath), - cell = tableView.dequeueReusableCellWithIdentifier(cellmodel.reuseIdentifier, forIndexPath: indexPath) as? Cell else { + let cell = tableView.dequeueReusableCell(withIdentifier: cellmodel.reuseIdentifier, for: indexPath) as? Cell else { return UITableViewCell() } @@ -476,11 +476,11 @@ extension Hakuba: UITableViewDataSource { return cell } - public func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { + public func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { commitEditingHandler?(editingStyle, indexPath) } - public func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { + public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { guard let cellmodel = getCellmodel(indexPath) else { return false } @@ -488,11 +488,11 @@ extension Hakuba: UITableViewDataSource { return cellmodel.editable || cellEditable } - public func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { + public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return sections.get(section)?.header?.title } - public func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? { + public func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { return sections.get(section)?.footer?.title } } @@ -500,11 +500,11 @@ extension Hakuba: UITableViewDataSource { // MARK - Private methods private extension Hakuba { - func setupSections(sections: [Section], fromIndex start: Int) { + func setupSections(_ sections: [Section], fromIndex start: Int) { var start = start sections.forEach { $0.setup(start, delegate: self) start += 1 } } -} \ No newline at end of file +} diff --git a/Hakuba/HakubaDelegate.swift b/Hakuba/HakubaDelegate.swift index 09ffb5c..3f60824 100644 --- a/Hakuba/HakubaDelegate.swift +++ b/Hakuba/HakubaDelegate.swift @@ -9,17 +9,17 @@ import UIKit @objc public protocol HakubaDelegate : class { - optional func scrollViewDidScroll(scrollView: UIScrollView) + @objc optional func scrollViewDidScroll(_ scrollView: UIScrollView) // Decelerating - optional func scrollViewWillBeginDecelerating(scrollView: UIScrollView) - optional func scrollViewDidEndDecelerating(scrollView: UIScrollView) + @objc optional func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) + @objc optional func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) - optional func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) - optional func scrollViewDidScrollToTop(scrollView: UIScrollView) + @objc optional func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) + @objc optional func scrollViewDidScrollToTop(_ scrollView: UIScrollView) // Draging - optional func scrollViewWillBeginDragging(scrollView: UIScrollView) - optional func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) - optional func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) + @objc optional func scrollViewWillBeginDragging(_ scrollView: UIScrollView) + @objc optional func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) + @objc optional func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) } diff --git a/Hakuba/HeaderFooterView.swift b/Hakuba/HeaderFooterView.swift index 008f1be..6119ca4 100644 --- a/Hakuba/HeaderFooterView.swift +++ b/Hakuba/HeaderFooterView.swift @@ -8,23 +8,23 @@ import UIKit -public class HeaderFooterView: UITableViewHeaderFooterView { +open class HeaderFooterView: UITableViewHeaderFooterView { weak var _viewmodel: HeaderFooterViewModel? - public func configureView(viewmodel: HeaderFooterViewModel) { + open func configureView(_ viewmodel: HeaderFooterViewModel) { _viewmodel = viewmodel configure() } - public func configure() { + open func configure() { } - public func didChangeFloatingState(isFloating: Bool, section: Int) { + open func didChangeFloatingState(_ isFloating: Bool, section: Int) { } - public func willDisplay(tableView: UITableView, section: Int) { + open func willDisplay(_ tableView: UITableView, section: Int) { } - public func didEndDisplaying(tableView: UITableView, section: Int) { + open func didEndDisplaying(_ tableView: UITableView, section: Int) { } } diff --git a/Hakuba/HeaderFooterViewModel.swift b/Hakuba/HeaderFooterViewModel.swift index 769b20c..186ee67 100644 --- a/Hakuba/HeaderFooterViewModel.swift +++ b/Hakuba/HeaderFooterViewModel.swift @@ -8,29 +8,29 @@ import UIKit -public class HeaderFooterViewModel { - public enum Type { - case Header - case Footer +open class HeaderFooterViewModel { + public enum `Type` { + case header + case footer } - public let reuseIdentifier: String + open let reuseIdentifier: String - public internal(set) var section: Int = 0 - public internal(set) var type: Type = .Header + open internal(set) var section: Int = 0 + open internal(set) var type: Type = .header - public var title: String? - public var height: CGFloat = 44 + open var title: String? + open var height: CGFloat = 44 - public var isHeader: Bool { - return type == .Header + open var isHeader: Bool { + return type == .header } - public var isFooter: Bool { - return type == .Footer + open var isFooter: Bool { + return type == .footer } - public init(view: T.Type) { + public init(view: T.Type) where T: HeaderFooterView, T: HeaderFooterViewType { self.reuseIdentifier = view.reuseIdentifier } } diff --git a/Hakuba/Label.swift b/Hakuba/Label.swift index 3ecb460..f43402a 100644 --- a/Hakuba/Label.swift +++ b/Hakuba/Label.swift @@ -8,8 +8,8 @@ import UIKit -public class Label : UILabel { - override public var bounds: CGRect { +open class Label : UILabel { + override open var bounds: CGRect { didSet { self.preferredMaxLayoutWidth = self.bounds.width } diff --git a/Hakuba/RangeExt.swift b/Hakuba/RangeExt.swift index ef00d7d..a41d11d 100644 --- a/Hakuba/RangeExt.swift +++ b/Hakuba/RangeExt.swift @@ -10,7 +10,7 @@ import Foundation extension NSRange { init(range: Range) { - self.location = range.startIndex - self.length = range.endIndex - range.startIndex + self.location = range.lowerBound + self.length = range.upperBound - range.lowerBound } -} \ No newline at end of file +} diff --git a/Hakuba/Section.swift b/Hakuba/Section.swift index ef09884..1c3cce4 100644 --- a/Hakuba/Section.swift +++ b/Hakuba/Section.swift @@ -9,13 +9,13 @@ import Foundation protocol SectionDelegate: class { - func bumpMe(type: SectionBumpType, animation: Animation) + func bumpMe(_ type: SectionBumpType, animation: Animation) } -public class Section { +open class Section { weak var delegate: SectionDelegate? - public private(set) var cellmodels: [CellModel] = [] - private let bumpTracker = BumpTracker() + open fileprivate(set) var cellmodels: [CellModel] = [] + fileprivate let bumpTracker = BumpTracker() internal(set) var index: Int = 0 @@ -23,21 +23,21 @@ public class Section { return bumpTracker.changed } - public var header: HeaderFooterViewModel? { + open var header: HeaderFooterViewModel? { didSet { header?.section = index - header?.type = .Header + header?.type = .header } } - public var footer: HeaderFooterViewModel? { + open var footer: HeaderFooterViewModel? { didSet { footer?.section = index - footer?.type = .Footer + footer?.type = .footer } } - public subscript(index: Int) -> CellModel? { + open subscript(index: Int) -> CellModel? { get { return cellmodels.get(index) } @@ -46,7 +46,7 @@ public class Section { public init() { } - public func bump(animation: Animation = .None) -> Self { + open func bump(_ animation: Animation = .none) -> Self { let type = bumpTracker.getSectionBumpType(index) delegate?.bumpMe(type, animation: animation) bumpTracker.didBump() @@ -64,11 +64,11 @@ public extension Section { return reset([]) } - func reset(cellmodel: CellModel) -> Self { + func reset(_ cellmodel: CellModel) -> Self { return reset([cellmodel]) } - func reset(cellmodels: [CellModel]) -> Self { + func reset(_ cellmodels: [CellModel]) -> Self { setupCellmodels(cellmodels, indexFrom: 0) self.cellmodels = cellmodels bumpTracker.didReset() @@ -77,27 +77,27 @@ public extension Section { // MARK - Append - func append(cellmodel: CellModel) -> Self { + func append(_ cellmodel: CellModel) -> Self { return append([cellmodel]) } - func append(cellmodels: [CellModel]) -> Self { + func append(_ cellmodels: [CellModel]) -> Self { return insert(cellmodels, atIndex: count) } // MARK - Insert - func insert(cellmodel: CellModel, atIndex index: Int) -> Self { + func insert(_ cellmodel: CellModel, atIndex index: Int) -> Self { return insert([cellmodel], atIndex: index) } - func insert(cellmodels: [CellModel], atIndex index: Int) -> Self { + func insert(_ cellmodels: [CellModel], atIndex index: Int) -> Self { guard cellmodels.isNotEmpty else { return self } let start = min(count, index) - self.cellmodels.insert(cellmodels, atIndex: start) + let _ = self.cellmodels.insert(cellmodels, atIndex: start) let affectedCellmodels = Array(self.cellmodels[start.. Self { + func insertBeforeLast(_ viewmodel: CellModel) -> Self { return insertBeforeLast([viewmodel]) } - func insertBeforeLast(viewmodels: [CellModel]) -> Self { + func insertBeforeLast(_ viewmodels: [CellModel]) -> Self { let index = max(cellmodels.count - 1, 0) return insert(viewmodels, atIndex: index) } // MARK - Remove - func remove(index: Int) -> Self { + func remove(_ index: Int) -> Self { return remove([index]) } - func remove(range: Range) -> Self { - let indexes = range.map { $0 } + func remove(_ range: CountableClosedRange) -> Self { + let indexes = [Int](range) return remove(indexes) } - func remove(indexes: [Int]) -> Self { + func remove(_ indexes: [Int]) -> Self { guard indexes.isNotEmpty else { return self } let sortedIndexes = indexes - .sort(<) + .sorted(by: <) .filter { $0 >= 0 && $0 < self.count } var remainCellmodels: [CellModel] = [] var i = 0 for j in 0.. Self { - let index = cellmodels.indexOf { return $0 === cellmodel } + func remove(_ cellmodel: CellModel) -> Self { + let index = cellmodels.index { return $0 === cellmodel } guard let i = index else { return self @@ -177,7 +177,7 @@ public extension Section { // MAKR - Move - func move(from: Int, to: Int) -> Self { + func move(_ from: Int, to: Int) -> Self { cellmodels.move(fromIndex: from, toIndex: to) setupCellmodels([cellmodels[from]], indexFrom: from) setupCellmodels([cellmodels[to]], indexFrom: to) @@ -214,7 +214,7 @@ public extension Section { // MARK - Internal methods extension Section { - func setup(index: Int, delegate: SectionDelegate) { + func setup(_ index: Int, delegate: SectionDelegate) { self.delegate = delegate self.index = index @@ -231,7 +231,7 @@ extension Section { // MARK - Private methods private extension Section { - func setupCellmodels(cellmodels: [CellModel], indexFrom start: Int) { + func setupCellmodels(_ cellmodels: [CellModel], indexFrom start: Int) { guard let delegate = delegate as? CellModelDelegate else { return } @@ -239,9 +239,9 @@ private extension Section { var start = start cellmodels.forEach { cellmodel in - let indexPath = NSIndexPath(forRow: start, inSection: index) + let indexPath = IndexPath(row: start, section: index) cellmodel.setup(indexPath, delegate: delegate) start += 1 } } -} \ No newline at end of file +} diff --git a/Hakuba/TableViewExt.swift b/Hakuba/TableViewExt.swift index 5328a4e..ed788f7 100644 --- a/Hakuba/TableViewExt.swift +++ b/Hakuba/TableViewExt.swift @@ -9,25 +9,25 @@ import UIKit extension UITableView { - func registerCellByNib(t: T.Type) { + func registerCellByNib(_ t: T.Type) { let nib = UINib(nibName: t.nibName, bundle: nil) - registerNib(nib, forCellReuseIdentifier: t.reuseIdentifier) + register(nib, forCellReuseIdentifier: t.reuseIdentifier) } - func registerCell(t: T.Type) { - registerClass(t, forCellReuseIdentifier: t.reuseIdentifier) + func registerCell(_ t: T.Type) { + register(t, forCellReuseIdentifier: t.reuseIdentifier) } - func registerHeaderFooterByNib(t: T.Type) { + func registerHeaderFooterByNib(_ t: T.Type) { let nib = UINib(nibName: t.nibName, bundle: nil) - registerNib(nib, forHeaderFooterViewReuseIdentifier: t.reuseIdentifier) + register(nib, forHeaderFooterViewReuseIdentifier: t.reuseIdentifier) } - func registerHeaderFooter(t: T.Type) { - registerClass(t, forHeaderFooterViewReuseIdentifier: t.reuseIdentifier) + func registerHeaderFooter(_ t: T.Type) { + register(t, forHeaderFooterViewReuseIdentifier: t.reuseIdentifier) } - func dequeueCell(t: T.Type, forIndexPath indexPath: NSIndexPath) -> T { - return dequeueReusableCellWithIdentifier(t.reuseIdentifier, forIndexPath: indexPath) as! T + func dequeueCell(_ t: T.Type, forIndexPath indexPath: IndexPath) -> T { + return dequeueReusableCell(withIdentifier: t.reuseIdentifier, for: indexPath) as! T } -} \ No newline at end of file +} From fa02c867ce9187661dcffc9228b8af863b54ad69 Mon Sep 17 00:00:00 2001 From: eskizyen Date: Thu, 13 Oct 2016 17:34:23 +0300 Subject: [PATCH 2/5] Swift3 conversion --- Hakuba/Cell.swift | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Hakuba/Cell.swift b/Hakuba/Cell.swift index a6e3585..f362f33 100644 --- a/Hakuba/Cell.swift +++ b/Hakuba/Cell.swift @@ -23,35 +23,35 @@ open class Cell: UITableViewCell { // MARK - Cell events public extension Cell { - func willDisplay(_ tableView: UITableView) { + public func willDisplay(_ tableView: UITableView) { } - func didEndDisplay(_ tableView: UITableView) { + public func didEndDisplay(_ tableView: UITableView) { } - func willSelect(_ tableView: UITableView, indexPath: IndexPath) -> IndexPath? { + public func willSelect(_ tableView: UITableView, indexPath: IndexPath) -> IndexPath? { return indexPath } - func didSelect(_ tableView: UITableView) { + public func didSelect(_ tableView: UITableView) { } - func willDeselect(_ tableView: UITableView, indexPath: IndexPath) -> IndexPath? { + public func willDeselect(_ tableView: UITableView, indexPath: IndexPath) -> IndexPath? { return indexPath } - func didDeselect(_ tableView: UITableView) { + public func didDeselect(_ tableView: UITableView) { } - func willBeginEditing(_ tableView: UITableView) { + public func willBeginEditing(_ tableView: UITableView) { } - func didEndEditing(_ tableView: UITableView) { + public func didEndEditing(_ tableView: UITableView) { } - func didHighlight(_ tableView: UITableView) { + public func didHighlight(_ tableView: UITableView) { } - func didUnhighlight(_ tableView: UITableView) { + public func didUnhighlight(_ tableView: UITableView) { } } From e02372719a5f742f31f3024d286abf038e4da82e Mon Sep 17 00:00:00 2001 From: eskizyen Date: Thu, 13 Oct 2016 17:38:21 +0300 Subject: [PATCH 3/5] Swift3 conversion --- Hakuba/Cell.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Hakuba/Cell.swift b/Hakuba/Cell.swift index f362f33..0b4ec50 100644 --- a/Hakuba/Cell.swift +++ b/Hakuba/Cell.swift @@ -23,7 +23,7 @@ open class Cell: UITableViewCell { // MARK - Cell events public extension Cell { - public func willDisplay(_ tableView: UITableView) { + open func willDisplay(_ tableView: UITableView) { } public func didEndDisplay(_ tableView: UITableView) { From e8781f430a51044a59a88575ce81582506c3b756 Mon Sep 17 00:00:00 2001 From: eskizyen Date: Thu, 13 Oct 2016 17:54:23 +0300 Subject: [PATCH 4/5] Swift3 conversion --- Hakuba/Cell.swift | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/Hakuba/Cell.swift b/Hakuba/Cell.swift index 0b4ec50..1348310 100644 --- a/Hakuba/Cell.swift +++ b/Hakuba/Cell.swift @@ -18,40 +18,38 @@ open class Cell: UITableViewCell { open func configure() { } -} - -// MARK - Cell events + + // MARK - Cell events -public extension Cell { open func willDisplay(_ tableView: UITableView) { } - public func didEndDisplay(_ tableView: UITableView) { + open func didEndDisplay(_ tableView: UITableView) { } - public func willSelect(_ tableView: UITableView, indexPath: IndexPath) -> IndexPath? { + open func willSelect(_ tableView: UITableView, indexPath: IndexPath) -> IndexPath? { return indexPath } - public func didSelect(_ tableView: UITableView) { + open func didSelect(_ tableView: UITableView) { } - public func willDeselect(_ tableView: UITableView, indexPath: IndexPath) -> IndexPath? { + open func willDeselect(_ tableView: UITableView, indexPath: IndexPath) -> IndexPath? { return indexPath } - public func didDeselect(_ tableView: UITableView) { + open func didDeselect(_ tableView: UITableView) { } - public func willBeginEditing(_ tableView: UITableView) { + open func willBeginEditing(_ tableView: UITableView) { } - public func didEndEditing(_ tableView: UITableView) { + open func didEndEditing(_ tableView: UITableView) { } - public func didHighlight(_ tableView: UITableView) { + open func didHighlight(_ tableView: UITableView) { } - public func didUnhighlight(_ tableView: UITableView) { + open func didUnhighlight(_ tableView: UITableView) { } } From 9d748747369671e162b4f088402eacbfa997121a Mon Sep 17 00:00:00 2001 From: eskizyen Date: Tue, 1 Nov 2016 14:22:03 +0300 Subject: [PATCH 5/5] @ra1028 review --- Example/Example.xcodeproj/project.pbxproj | 37 ++++++++------ Example/Example/CellTestViewController.swift | 4 +- Example/Example/CustomCell.swift | 2 +- .../HeaderFooterTestViewController.swift | 4 +- Example/Example/ViewController.swift | 2 +- Hakuba/ArrayExt.swift | 2 +- Hakuba/Hakuba+Registration.swift | 6 +-- Hakuba/Hakuba+UIScrollViewDelegate.swift | 2 +- Hakuba/Hakuba.swift | 49 ++++++++++--------- Hakuba/Section.swift | 41 +++++++++------- 10 files changed, 82 insertions(+), 67 deletions(-) diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index 8d73ace..6cd74cd 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -133,22 +133,22 @@ 8C30CA4A1C8E71880008B717 /* Hakuba */ = { isa = PBXGroup; children = ( - 8C30CA4C1C8E71880008B717 /* Common.swift */, + 8C30CA4B1C8E71880008B717 /* ArrayExt.swift */, 8C30CA4D1C8E71880008B717 /* BumpTracker.swift */, - 8CFF14811C9568C9005699CE /* CellType.swift */, 8C30CA4E1C8E71880008B717 /* Cell.swift */, 8C30CA4F1C8E71880008B717 /* CellModel.swift */, - 8C5820DA1C984FDD009FB9AE /* HeaderFooterViewType.swift */, - 8C30CA501C8E71880008B717 /* HeaderFooterView.swift */, - 8C30CA511C8E71880008B717 /* HeaderFooterViewModel.swift */, - 8C30CA571C8E71880008B717 /* Section.swift */, + 8CFF14811C9568C9005699CE /* CellType.swift */, + 8C30CA4C1C8E71880008B717 /* Common.swift */, 8C30CA541C8E71880008B717 /* Hakuba.swift */, 8C30CA521C8E71880008B717 /* Hakuba+Registration.swift */, 8C30CA531C8E71880008B717 /* Hakuba+UIScrollViewDelegate.swift */, 8C30CA551C8E71880008B717 /* HakubaDelegate.swift */, + 8C30CA501C8E71880008B717 /* HeaderFooterView.swift */, + 8C30CA511C8E71880008B717 /* HeaderFooterViewModel.swift */, + 8C5820DA1C984FDD009FB9AE /* HeaderFooterViewType.swift */, 8C30CA561C8E71880008B717 /* Label.swift */, - 8C30CA4B1C8E71880008B717 /* ArrayExt.swift */, 8C30CA581C8E71880008B717 /* RangeExt.swift */, + 8C30CA571C8E71880008B717 /* Section.swift */, 8C30CA591C8E71880008B717 /* TableViewCellExt.swift */, 8C30CA5A1C8E71880008B717 /* TableViewExt.swift */, 8C30CA5B1C8E71880008B717 /* TableViewHeaderFooterViewExt.swift */, @@ -182,20 +182,20 @@ isa = PBXGroup; children = ( 8C60BC5E1C89D4550006CE38 /* AppDelegate.swift */, - 8C60BC601C89D4550006CE38 /* ViewController.swift */, - 8C5820DC1C9852E3009FB9AE /* HeaderFooterTestViewController.swift */, + 8C60BC651C89D4550006CE38 /* Assets.xcassets */, 8C5820DE1C9852F7009FB9AE /* CellTestViewController.swift */, + 8C3446801C8E6F97002BC8CD /* ChildViewController.swift */, 8C3446811C8E6F97002BC8CD /* CustomCell.swift */, - 8CFF14A51C96758A005699CE /* CustomCellModel.swift */, 8C3446821C8E6F97002BC8CD /* CustomCell.xib */, - 8C3446801C8E6F97002BC8CD /* ChildViewController.swift */, + 8CFF14A51C96758A005699CE /* CustomCellModel.swift */, 8C5820E01C985625009FB9AE /* CustomHeaderView.swift */, - 8C5820E21C98562F009FB9AE /* CustomHeaderViewModel.swift */, 8C5820E41C985640009FB9AE /* CustomHeaderView.xib */, - 8C60BC621C89D4550006CE38 /* Main.storyboard */, - 8C60BC651C89D4550006CE38 /* Assets.xcassets */, - 8C60BC671C89D4550006CE38 /* LaunchScreen.storyboard */, + 8C5820E21C98562F009FB9AE /* CustomHeaderViewModel.swift */, + 8C5820DC1C9852E3009FB9AE /* HeaderFooterTestViewController.swift */, 8C60BC6A1C89D4550006CE38 /* Info.plist */, + 8C60BC671C89D4550006CE38 /* LaunchScreen.storyboard */, + 8C60BC621C89D4550006CE38 /* Main.storyboard */, + 8C60BC601C89D4550006CE38 /* ViewController.swift */, ); path = Example; sourceTree = ""; @@ -281,7 +281,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0810; TargetAttributes = { 8C60BC5A1C89D4550006CE38 = { CreatedOnToolsVersion = 7.2; @@ -448,8 +448,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -493,8 +495,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -513,6 +517,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; }; diff --git a/Example/Example/CellTestViewController.swift b/Example/Example/CellTestViewController.swift index 74874d5..b285c91 100644 --- a/Example/Example/CellTestViewController.swift +++ b/Example/Example/CellTestViewController.swift @@ -25,7 +25,7 @@ class CellTestViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - hakuba.registerCellByNib(CustomCell) + hakuba.registerCellByNib(CustomCell.self) // Top section @@ -39,7 +39,7 @@ class CellTestViewController: UIViewController { } hakuba - .reset(SectionIndex) + .reset(SectionIndex.self) .bump() let topSection = hakuba[SectionIndex.top] diff --git a/Example/Example/CustomCell.swift b/Example/Example/CustomCell.swift index 673cecc..3ec7570 100644 --- a/Example/Example/CustomCell.swift +++ b/Example/Example/CustomCell.swift @@ -18,7 +18,7 @@ class CustomCell: Cell, CellType { return } - titleLabel.text = cellmodel.title + "(\((cellmodel.indexPath as NSIndexPath).section),\((cellmodel.indexPath as NSIndexPath).row))" + titleLabel.text = cellmodel.title + "(\(cellmodel.indexPath.section),\(cellmodel.indexPath.row))" } override func willDisplay(_ tableView: UITableView) { diff --git a/Example/Example/HeaderFooterTestViewController.swift b/Example/Example/HeaderFooterTestViewController.swift index 369fff8..542aeb0 100644 --- a/Example/Example/HeaderFooterTestViewController.swift +++ b/Example/Example/HeaderFooterTestViewController.swift @@ -18,8 +18,8 @@ class HeaderFooterTestViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - hakuba.registerCellByNib(CustomCell) - .registerHeaderFooterByNib(CustomHeaderView) + hakuba.registerCellByNib(CustomCell.self) + .registerHeaderFooterByNib(CustomHeaderView.self) // Top section diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index fdc116c..c9cc67d 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -18,7 +18,7 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - hakuba.registerCellByNib(CustomCell) + hakuba.registerCellByNib(CustomCell.self) let cm1 = CustomCellModel(title: "Test cell") { [weak self] _ in let storyboard = UIStoryboard(name: "Main", bundle: nil) diff --git a/Hakuba/ArrayExt.swift b/Hakuba/ArrayExt.swift index f4ac2b6..3ad1e56 100644 --- a/Hakuba/ArrayExt.swift +++ b/Hakuba/ArrayExt.swift @@ -49,7 +49,7 @@ extension Array { return start.. Bool { + @discardableResult mutating func move(fromIndex from: Int, toIndex to: Int) -> Bool { if !hasIndex(from) || !hasIndex(to) || from == to { return false } diff --git a/Hakuba/Hakuba+Registration.swift b/Hakuba/Hakuba+Registration.swift index c864d2e..5787577 100644 --- a/Hakuba/Hakuba+Registration.swift +++ b/Hakuba/Hakuba+Registration.swift @@ -9,7 +9,7 @@ import UIKit public extension Hakuba { - func registerCellByNib(_ cellType: T.Type) -> Self where T: Cell, T: CellType { + @discardableResult func registerCellByNib(_ cellType: T.Type) -> Self where T: Cell, T: CellType { tableView?.registerCellByNib(cellType) return self } @@ -21,7 +21,7 @@ public extension Hakuba { return self } - func registerCell(_ cellType: T.Type) -> Self { + @discardableResult func registerCell(_ cellType: T.Type) -> Self { tableView?.registerCell(cellType) return self } @@ -33,7 +33,7 @@ public extension Hakuba { return self } - func registerHeaderFooterByNib(_ t: T.Type) -> Self where T: HeaderFooterView, T: HeaderFooterViewType { + @discardableResult func registerHeaderFooterByNib(_ t: T.Type) -> Self where T: HeaderFooterView, T: HeaderFooterViewType { tableView?.registerHeaderFooterByNib(t) return self } diff --git a/Hakuba/Hakuba+UIScrollViewDelegate.swift b/Hakuba/Hakuba+UIScrollViewDelegate.swift index 1a4a3c4..c8e3fef 100644 --- a/Hakuba/Hakuba+UIScrollViewDelegate.swift +++ b/Hakuba/Hakuba+UIScrollViewDelegate.swift @@ -14,7 +14,7 @@ extension Hakuba { delegate?.scrollViewDidScroll?(scrollView) if let indexPath = tableView?.indexPathsForVisibleRows?.first { - let topSection = (indexPath as NSIndexPath).section + let topSection = indexPath.section if currentTopSection != topSection { if let headerView = tableView?.headerView(forSection: currentTopSection) as? HeaderFooterView { diff --git a/Hakuba/Hakuba.swift b/Hakuba/Hakuba.swift index 6e17db9..bb249ac 100644 --- a/Hakuba/Hakuba.swift +++ b/Hakuba/Hakuba.swift @@ -62,11 +62,11 @@ final public class Hakuba: NSObject { } subscript(indexPath: IndexPath) -> CellModel? { - return self[(indexPath as NSIndexPath).section][(indexPath as NSIndexPath).row] + return self[indexPath.section][(indexPath as NSIndexPath).row] } public func getCellmodel(_ indexPath: IndexPath) -> CellModel? { - return sections.get((indexPath as NSIndexPath).section)?[(indexPath as NSIndexPath).row] + return sections.get(indexPath.section)?[(indexPath as NSIndexPath).row] } public func getSection(_ index: Int) -> Section? { @@ -89,7 +89,7 @@ final public class Hakuba: NSObject { tableView?.dataSource = nil } - public func bump(_ animation: Animation = .none) -> Self { + @discardableResult public func bump(_ animation: Animation = .none) -> Self { let changedCount = sections.reduce(0) { $0 + ($1.changed ? 1 : 0) } if changedCount == 0 { @@ -148,20 +148,20 @@ public extension Hakuba { public extension Hakuba { // MARK - Reset - func reset(_ listType: SectionIndexType.Type) -> Self { + @discardableResult func reset(_ listType: SectionIndexType.Type) -> Self { let sections = (0.. Self { + @discardableResult func reset() -> Self { return reset([]) } - func reset(_ section: Section) -> Self { + @discardableResult func reset(_ section: Section) -> Self { return reset([section]) } - func reset(_ sections: [Section]) -> Self { + @discardableResult func reset(_ sections: [Section]) -> Self { setupSections(sections, fromIndex: 0) self.sections = sections bumpTracker.didReset() @@ -170,21 +170,21 @@ public extension Hakuba { // MARK - Append - func append(_ section: Section) -> Self { + @discardableResult func append(_ section: Section) -> Self { return append([section]) } - func append(_ sections: [Section]) -> Self { + @discardableResult func append(_ sections: [Section]) -> Self { return insert(sections, atIndex: sectionsCount) } // MARK - Insert - func insert(_ section: Section, atIndex index: Int) -> Self { + @discardableResult func insert(_ section: Section, atIndex index: Int) -> Self { return insert([section], atIndex: index) } - func insert(_ sections: [Section], atIndex index: Int) -> Self { + @discardableResult func insert(_ sections: [Section], atIndex index: Int) -> Self { guard sections.isNotEmpty else { return self } @@ -197,27 +197,32 @@ public extension Hakuba { return self } - func insertBeforeLast(_ section: Section) -> Self { + @discardableResult func insertBeforeLast(_ section: Section) -> Self { return insertBeforeLast([section]) } - func insertBeforeLast(_ sections: [Section]) -> Self { + @discardableResult func insertBeforeLast(_ sections: [Section]) -> Self { let index = max(sections.count - 1, 0) return insert(sections, atIndex: index) } // MARK - Remove - func remove(_ index: Int) -> Self { + @discardableResult func remove(_ index: Int) -> Self { return remove(indexes: [index]) } - func remove(_ range: CountableClosedRange) -> Self { + @discardableResult func remove(_ range: CountableRange) -> Self { let indexes = range.map { $0 } return remove(indexes: indexes) } - func remove(indexes: [Int]) -> Self { + @discardableResult func remove(_ range: CountableClosedRange) -> Self { + let indexes = range.map { $0 } + return remove(indexes: indexes) + } + + @discardableResult func remove(indexes: [Int]) -> Self { guard indexes.isNotEmpty else { return self } @@ -230,7 +235,7 @@ public extension Hakuba { var i = 0 for j in 0.. Self { + @discardableResult func removeLast() -> Self { let index = sectionsCount - 1 guard index >= 0 else { @@ -255,7 +260,7 @@ public extension Hakuba { return remove(index) } - func remove(_ section: Section) -> Self { + @discardableResult func remove(_ section: Section) -> Self { let index = section.index guard index >= 0 && index < sectionsCount else { @@ -265,13 +270,13 @@ public extension Hakuba { return remove(index) } - func removeAll() -> Self { + @discardableResult func removeAll() -> Self { return reset() } // MAKR - Move - func move(_ from: Int, to: Int) -> Self { + @discardableResult func move(_ from: Int, to: Int) -> Self { sections.move(fromIndex: from, toIndex: to) setupSections([sections[from]], fromIndex: from) setupSections([sections[to]], fromIndex: to) @@ -420,7 +425,7 @@ extension Hakuba { } public func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { - guard let view = view as? HeaderFooterView , section == willFloatingSection else { + guard let view = view as? HeaderFooterView, section == willFloatingSection else { return } diff --git a/Hakuba/Section.swift b/Hakuba/Section.swift index 1c3cce4..e886e5d 100644 --- a/Hakuba/Section.swift +++ b/Hakuba/Section.swift @@ -46,7 +46,7 @@ open class Section { public init() { } - open func bump(_ animation: Animation = .none) -> Self { + @discardableResult open func bump(_ animation: Animation = .none) -> Self { let type = bumpTracker.getSectionBumpType(index) delegate?.bumpMe(type, animation: animation) bumpTracker.didBump() @@ -60,15 +60,15 @@ public extension Section { // MARK - Reset - func reset() -> Self { + @discardableResult func reset() -> Self { return reset([]) } - func reset(_ cellmodel: CellModel) -> Self { + @discardableResult func reset(_ cellmodel: CellModel) -> Self { return reset([cellmodel]) } - func reset(_ cellmodels: [CellModel]) -> Self { + @discardableResult func reset(_ cellmodels: [CellModel]) -> Self { setupCellmodels(cellmodels, indexFrom: 0) self.cellmodels = cellmodels bumpTracker.didReset() @@ -77,21 +77,21 @@ public extension Section { // MARK - Append - func append(_ cellmodel: CellModel) -> Self { + @discardableResult func append(_ cellmodel: CellModel) -> Self { return append([cellmodel]) } - func append(_ cellmodels: [CellModel]) -> Self { + @discardableResult func append(_ cellmodels: [CellModel]) -> Self { return insert(cellmodels, atIndex: count) } // MARK - Insert - func insert(_ cellmodel: CellModel, atIndex index: Int) -> Self { + @discardableResult func insert(_ cellmodel: CellModel, atIndex index: Int) -> Self { return insert([cellmodel], atIndex: index) } - func insert(_ cellmodels: [CellModel], atIndex index: Int) -> Self { + @discardableResult func insert(_ cellmodels: [CellModel], atIndex index: Int) -> Self { guard cellmodels.isNotEmpty else { return self } @@ -108,27 +108,32 @@ public extension Section { return self } - func insertBeforeLast(_ viewmodel: CellModel) -> Self { + @discardableResult func insertBeforeLast(_ viewmodel: CellModel) -> Self { return insertBeforeLast([viewmodel]) } - func insertBeforeLast(_ viewmodels: [CellModel]) -> Self { + @discardableResult func insertBeforeLast(_ viewmodels: [CellModel]) -> Self { let index = max(cellmodels.count - 1, 0) return insert(viewmodels, atIndex: index) } // MARK - Remove - func remove(_ index: Int) -> Self { + @discardableResult func remove(_ index: Int) -> Self { return remove([index]) } - - func remove(_ range: CountableClosedRange) -> Self { + + @discardableResult func remove(_ range: CountableRange) -> Self { + let indexes = [Int](range) + return remove(indexes) + } + + @discardableResult func remove(_ range: CountableClosedRange) -> Self { let indexes = [Int](range) return remove(indexes) } - func remove(_ indexes: [Int]) -> Self { + @discardableResult func remove(_ indexes: [Int]) -> Self { guard indexes.isNotEmpty else { return self } @@ -141,7 +146,7 @@ public extension Section { var i = 0 for j in 0.. Self { + @discardableResult func removeLast() -> Self { let index = cellmodels.count - 1 guard index >= 0 else { return self @@ -165,7 +170,7 @@ public extension Section { return remove(index) } - func remove(_ cellmodel: CellModel) -> Self { + @discardableResult func remove(_ cellmodel: CellModel) -> Self { let index = cellmodels.index { return $0 === cellmodel } guard let i = index else { @@ -177,7 +182,7 @@ public extension Section { // MAKR - Move - func move(_ from: Int, to: Int) -> Self { + @discardableResult func move(_ from: Int, to: Int) -> Self { cellmodels.move(fromIndex: from, toIndex: to) setupCellmodels([cellmodels[from]], indexFrom: from) setupCellmodels([cellmodels[to]], indexFrom: to)