From c26b292135b2cf32c65ae9d56e4e107bdb0e6c19 Mon Sep 17 00:00:00 2001 From: John Flanagan Date: Tue, 12 Sep 2017 22:57:27 -0500 Subject: [PATCH] Migrate to Swift 4.0 --- MiniKeePass.xcodeproj/project.pbxproj | 8 ++- .../CustomFieldViewController.swift | 6 +- MiniKeePass/DatabaseManager.m | 4 +- .../Group View/GroupViewController.swift | 12 ++-- .../ImageSelectorViewController.swift | 4 +- .../KeyFileViewController.swift | 26 ++++---- .../PasswordEntryViewController.swift | 61 +++++++++---------- .../PasswordGeneratorViewController.swift | 4 +- .../WebBrowserViewController.swift | 4 +- 9 files changed, 64 insertions(+), 65 deletions(-) diff --git a/MiniKeePass.xcodeproj/project.pbxproj b/MiniKeePass.xcodeproj/project.pbxproj index 0b7f568c..76784bc1 100644 --- a/MiniKeePass.xcodeproj/project.pbxproj +++ b/MiniKeePass.xcodeproj/project.pbxproj @@ -891,7 +891,7 @@ TargetAttributes = { 689DFFD413A4273F005EBD36 = { DevelopmentTeam = G9ZT7943FZ; - LastSwiftMigration = 0830; + LastSwiftMigration = 0900; SystemCapabilities = { com.apple.iCloud = { enabled = 0; @@ -1259,7 +1259,8 @@ PROVISIONING_PROFILE_SPECIFIER = "Flush Software Development: *"; SWIFT_OBJC_BRIDGING_HEADER = "MiniKeePass/MiniKeePass-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Off; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -1281,7 +1282,8 @@ PROVISIONING_PROFILE = "5443260a-ff69-4503-8b4b-2b598dc5db88"; PROVISIONING_PROFILE_SPECIFIER = "Flush Software Development: *"; SWIFT_OBJC_BRIDGING_HEADER = "MiniKeePass/MiniKeePass-Bridging-Header.h"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = Off; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; diff --git a/MiniKeePass/Custom Field View/CustomFieldViewController.swift b/MiniKeePass/Custom Field View/CustomFieldViewController.swift index f59f2f95..6f2b8606 100644 --- a/MiniKeePass/Custom Field View/CustomFieldViewController.swift +++ b/MiniKeePass/Custom Field View/CustomFieldViewController.swift @@ -22,10 +22,10 @@ class CustomFieldViewController: UITableViewController { @IBOutlet weak var valueTextField: UITextField! @IBOutlet weak var inMemoryProtectionSwitch: UISwitch! - var stringField: StringField? + @objc var stringField: StringField? - var donePressed: ((_ customFieldViewController: CustomFieldViewController) -> Void)? - var cancelPressed: ((_ customFieldViewController: CustomFieldViewController) -> Void)? + @objc var donePressed: ((_ customFieldViewController: CustomFieldViewController) -> Void)? + @objc var cancelPressed: ((_ customFieldViewController: CustomFieldViewController) -> Void)? override func viewDidLoad() { super.viewDidLoad() diff --git a/MiniKeePass/DatabaseManager.m b/MiniKeePass/DatabaseManager.m index eadc99ab..fe6824db 100644 --- a/MiniKeePass/DatabaseManager.m +++ b/MiniKeePass/DatabaseManager.m @@ -245,13 +245,13 @@ - (void)openDatabaseWithPasswordEntryViewController:(PasswordEntryViewController NSString *path = [documentsDirectory stringByAppendingPathComponent:self.selectedFilename]; // Get the password - NSString *password = [passwordEntryViewController getPassword]; + NSString *password = passwordEntryViewController.password; if ([password isEqualToString:@""]) { password = nil; } // Get the keyfile - NSString *keyFile = [passwordEntryViewController getKeyFile]; + NSString *keyFile = passwordEntryViewController.keyFile; NSString *keyFilePath = nil; if (keyFile != nil) { NSString *documentsDirectory = [AppDelegate documentsDirectory]; diff --git a/MiniKeePass/Group View/GroupViewController.swift b/MiniKeePass/Group View/GroupViewController.swift index a7aa98ba..d70f7889 100755 --- a/MiniKeePass/Group View/GroupViewController.swift +++ b/MiniKeePass/Group View/GroupViewController.swift @@ -354,7 +354,7 @@ class GroupViewController: UITableViewController { // MARK: - Actions - func settingsPressed(sender: UIBarButtonItem) { + @objc func settingsPressed(sender: UIBarButtonItem) { let storyboard = UIStoryboard(name: "Settings", bundle: nil) guard let viewController = storyboard.instantiateInitialViewController() else { return @@ -363,7 +363,7 @@ class GroupViewController: UITableViewController { present(viewController, animated: true, completion: nil) } - func actionPressed(sender: UIBarButtonItem) { + @objc func actionPressed(sender: UIBarButtonItem) { // Get the URL of the database guard let appDelegate = AppDelegate.getDelegate() else { return @@ -381,7 +381,7 @@ class GroupViewController: UITableViewController { } } - func addPressed(sender: UIBarButtonItem) { + @objc func addPressed(sender: UIBarButtonItem) { let alertController = UIAlertController(title: NSLocalizedString("Add", comment: ""), message: nil, preferredStyle: .alert) // Add an action to add a new group @@ -489,13 +489,13 @@ class GroupViewController: UITableViewController { navigationController?.pushViewController(viewController, animated: true) } - func deletePressed(sender: UIBarButtonItem) { + @objc func deletePressed(sender: UIBarButtonItem) { if let indexPaths = tableView.indexPathsForSelectedRows { deleteItems(indexPaths: indexPaths) } } - func movePressed(sender: UIBarButtonItem) { + @objc func movePressed(sender: UIBarButtonItem) { guard let indexPaths = tableView.indexPathsForSelectedRows else { // Nothing selected. Shouldn't have been possible to press "Move" return; @@ -545,7 +545,7 @@ class GroupViewController: UITableViewController { present(navigationController, animated: true, completion: nil) } - func renamePressed(sender: UIBarButtonItem) { + @objc func renamePressed(sender: UIBarButtonItem) { guard let indexPath = tableView.indexPathForSelectedRow else { // Nothing selected. This shoudn't have been called return diff --git a/MiniKeePass/Image Selector View/ImageSelectorViewController.swift b/MiniKeePass/Image Selector View/ImageSelectorViewController.swift index 3771ad59..7470078c 100644 --- a/MiniKeePass/Image Selector View/ImageSelectorViewController.swift +++ b/MiniKeePass/Image Selector View/ImageSelectorViewController.swift @@ -23,9 +23,9 @@ class ImageSelectorViewController: UICollectionViewController { private let reuseIdentifier = "ImageCell" private var images: [UIImage] = [] - var selectedImage = -1 + @objc var selectedImage = -1 - var imageSelected: ((_ imageSelectorViewController: ImageSelectorViewController, _ selectedImage: Int) -> Void)? + @objc var imageSelected: ((_ imageSelectorViewController: ImageSelectorViewController, _ selectedImage: Int) -> Void)? override func viewDidLoad() { super.viewDidLoad() diff --git a/MiniKeePass/Password Entry View/KeyFileViewController.swift b/MiniKeePass/Password Entry View/KeyFileViewController.swift index 42117217..9a3f7751 100644 --- a/MiniKeePass/Password Entry View/KeyFileViewController.swift +++ b/MiniKeePass/Password Entry View/KeyFileViewController.swift @@ -19,9 +19,9 @@ import UIKit class KeyFileViewController: UITableViewController { var keyFiles: [String]! - var selectedIndex: Int! + var selectedKeyIndex: Int? - var keyFileSelected: ((_ selectedIndex: Int) -> Void)? + var keyFileSelected: ((_ selectedIndex: Int?) -> Void)? // MARK: - Table view data source @@ -35,31 +35,33 @@ class KeyFileViewController: UITableViewController { } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let idx = indexPath.row - 1 - let cell = tableView.dequeueReusableCell(withIdentifier: "KeyFileCell", for: indexPath) - if (idx == -1) { + if (indexPath.row == 0) { cell.textLabel?.text = NSLocalizedString("None", comment: "") + cell.accessoryType = (selectedKeyIndex == nil ? .checkmark : .none) } else { - cell.textLabel?.text = keyFiles[idx] + let keyIndex = indexPath.row - 1 + cell.textLabel?.text = keyFiles[keyIndex] + cell.accessoryType = (keyIndex == selectedKeyIndex ? .checkmark : .none) } - cell.accessoryType = idx == selectedIndex ? .checkmark : .none return cell } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - let idx = indexPath.row - 1 - if (idx != selectedIndex) { - let oldCell = tableView.cellForRow(at: IndexPath(row: selectedIndex + 1, section: 0)) + let keyIndex = indexPath.row > 0 ? indexPath.row - 1 : nil + let oldIndexPath = IndexPath(row: (selectedKeyIndex ?? -1) + 1, section: 0) + + if (indexPath != oldIndexPath) { + let oldCell = tableView.cellForRow(at: oldIndexPath) oldCell!.accessoryType = .none let cell = tableView.cellForRow(at: indexPath) cell!.accessoryType = .checkmark - selectedIndex = idx + selectedKeyIndex = keyIndex - keyFileSelected?(selectedIndex) + keyFileSelected?(selectedKeyIndex) } tableView.deselectRow(at: indexPath, animated: true) diff --git a/MiniKeePass/Password Entry View/PasswordEntryViewController.swift b/MiniKeePass/Password Entry View/PasswordEntryViewController.swift index 810baea7..fb0446f7 100644 --- a/MiniKeePass/Password Entry View/PasswordEntryViewController.swift +++ b/MiniKeePass/Password Entry View/PasswordEntryViewController.swift @@ -22,13 +22,33 @@ class PasswordEntryViewController: UITableViewController, UITextFieldDelegate { @IBOutlet weak var showImageView: UIImageView! @IBOutlet weak var keyFileLabel: UILabel! - var filename: String! + @objc var filename: String! - var keyFiles: [String]! - fileprivate var selectedKeyFileIndex: Int! = -1 + @objc var keyFiles: [String]! + fileprivate var selectedKeyFileIndex: Int? = nil { + didSet { + if let selectedKeyFileIndex = selectedKeyFileIndex { + keyFileLabel.text = keyFiles[selectedKeyFileIndex] + } else { + keyFileLabel.text = NSLocalizedString("None", comment: "") + } + } + } + + @objc var keyFile: String? { + guard let selectedKeyFileIndex = selectedKeyFileIndex else { + return nil + } + + return keyFiles[selectedKeyFileIndex] + } + + @objc var password: String! { + return passwordTextField.text + } - var donePressed: ((PasswordEntryViewController) -> Void)? - var cancelPressed: ((PasswordEntryViewController) -> Void)? + @objc var donePressed: ((PasswordEntryViewController) -> Void)? + @objc var cancelPressed: ((PasswordEntryViewController) -> Void)? override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) @@ -36,37 +56,12 @@ class PasswordEntryViewController: UITableViewController, UITextFieldDelegate { if (keyFileLabel.text == "") { let keyFile = ((filename as NSString).deletingPathExtension as NSString).appendingPathExtension("key") let idx = keyFiles.index(of: keyFile!) - setSelectedKeyFile(idx) + selectedKeyFileIndex = idx } passwordTextField.becomeFirstResponder() } - func getPassword() -> String! { - return passwordTextField.text - } - - func getKeyFile() -> String! { - if (selectedKeyFileIndex == -1) { - return nil - } - return keyFiles[selectedKeyFileIndex] - } - - func setSelectedKeyFile(_ selectedIndex: Int!) -> Void { - if (selectedIndex == nil) { - selectedKeyFileIndex = -1 - } else { - selectedKeyFileIndex = selectedIndex - } - - if (selectedKeyFileIndex == -1) { - keyFileLabel.text = NSLocalizedString("None", comment: "") - } else { - keyFileLabel.text = keyFiles[selectedKeyFileIndex] - } - } - // MARK: - UITextFieldDelegate func textFieldShouldReturn(_ textField: UITextField) -> Bool { @@ -88,9 +83,9 @@ class PasswordEntryViewController: UITableViewController, UITextFieldDelegate { override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let keyFileViewController = segue.destination as! KeyFileViewController keyFileViewController.keyFiles = keyFiles - keyFileViewController.selectedIndex = selectedKeyFileIndex + keyFileViewController.selectedKeyIndex = selectedKeyFileIndex keyFileViewController.keyFileSelected = { (selectedIndex) in - self.setSelectedKeyFile(selectedIndex) + self.selectedKeyFileIndex = selectedIndex keyFileViewController.navigationController?.popViewController(animated: true) } diff --git a/MiniKeePass/Password Generator View/PasswordGeneratorViewController.swift b/MiniKeePass/Password Generator View/PasswordGeneratorViewController.swift index 832332f7..b49bbc68 100644 --- a/MiniKeePass/Password Generator View/PasswordGeneratorViewController.swift +++ b/MiniKeePass/Password Generator View/PasswordGeneratorViewController.swift @@ -38,8 +38,8 @@ class PasswordGeneratorViewController: UITableViewController, UIPickerViewDataSo fileprivate var length: Int = 0 fileprivate var charSets: Int = 10 - var donePressed: ((PasswordGeneratorViewController, _ password: String) -> Void)? - var cancelPressed: ((PasswordGeneratorViewController) -> Void)? + @objc var donePressed: ((PasswordGeneratorViewController, _ password: String) -> Void)? + @objc var cancelPressed: ((PasswordGeneratorViewController) -> Void)? override func viewDidLoad() { super.viewDidLoad() diff --git a/MiniKeePass/Web Browser View/WebBrowserViewController.swift b/MiniKeePass/Web Browser View/WebBrowserViewController.swift index 9a42d48f..38708a94 100644 --- a/MiniKeePass/Web Browser View/WebBrowserViewController.swift +++ b/MiniKeePass/Web Browser View/WebBrowserViewController.swift @@ -26,8 +26,8 @@ class WebBrowserViewController: UIViewController, WKNavigationDelegate { fileprivate var webView: WKWebView! - var url: URL? - var entry: KdbEntry? + @objc var url: URL? + @objc var entry: KdbEntry? override func loadView() { super.loadView()