From e29d22e89cec77d5e1012d59a10e13a61cf098d0 Mon Sep 17 00:00:00 2001 From: Roy Marmelstein Date: Sat, 27 Aug 2016 17:41:09 +0300 Subject: [PATCH] Swift 3 updates --- .travis.yml | 2 +- .../Sample/Sample.xcodeproj/project.pbxproj | 6 ++ examples/Sample/Sample/AppDelegate.swift | 28 ++++----- examples/Sample/Sample/FileBrowser.swift | 60 +++++++++---------- examples/Sample/SampleTests/SampleTests.swift | 2 +- 5 files changed, 52 insertions(+), 46 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6a7e71c..9f5fcbb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ env: - LANG=en_US.UTF-8 before_install: - brew update || brew update - - gem install cocoapods + - gem install cocoapods --pre install: echo "<3" env: - MODE=framework diff --git a/examples/Sample/Sample.xcodeproj/project.pbxproj b/examples/Sample/Sample.xcodeproj/project.pbxproj index 7aaa0e3..9af2bc3 100644 --- a/examples/Sample/Sample.xcodeproj/project.pbxproj +++ b/examples/Sample/Sample.xcodeproj/project.pbxproj @@ -172,9 +172,11 @@ TargetAttributes = { 3430F66C1C45C930007473A6 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; }; 3430F6801C45C930007473A6 = { CreatedOnToolsVersion = 7.2; + LastSwiftMigration = 0800; TestTargetID = 3430F66C1C45C930007473A6; }; }; @@ -359,6 +361,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.roymarmelstein.Sample; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -370,6 +373,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.roymarmelstein.Sample; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -381,6 +385,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.roymarmelstein.SampleTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Sample.app/Sample"; }; name = Debug; @@ -393,6 +398,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = com.roymarmelstein.SampleTests; PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 3.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/Sample.app/Sample"; }; name = Release; diff --git a/examples/Sample/Sample/AppDelegate.swift b/examples/Sample/Sample/AppDelegate.swift index 4d19384..41e387b 100644 --- a/examples/Sample/Sample/AppDelegate.swift +++ b/examples/Sample/Sample/AppDelegate.swift @@ -14,18 +14,18 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { - if NSUserDefaults.standardUserDefaults().boolForKey("firstLaunch") == false { - NSUserDefaults.standardUserDefaults().setBool(true, forKey: "firstLaunch") - NSUserDefaults.standardUserDefaults().synchronize() - let fileManager = NSFileManager.defaultManager() + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: Any]?) -> Bool { + if UserDefaults.standard.bool(forKey: "firstLaunch") == false { + UserDefaults.standard.set(true, forKey: "firstLaunch") + UserDefaults.standard.synchronize() + let fileManager = FileManager.default let fileNames = ["Image1.jpg", "Image2.jpg", "Image3.jpg", "Images.zip"] - let documentsUrl = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL - let bundleUrl = NSBundle.mainBundle().resourceURL + let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL + let bundleUrl = Bundle.main.resourceURL for file in fileNames { - if let srcPath = bundleUrl?.URLByAppendingPathComponent(file).path, let toPath = documentsUrl.URLByAppendingPathComponent(file).path{ + if let srcPath = bundleUrl?.appendingPathComponent(file).path, let toPath = documentsUrl.appendingPathComponent(file).path{ do { - try fileManager.copyItemAtPath(srcPath, toPath: toPath) + try fileManager.copyItem(atPath: srcPath, toPath: toPath) } catch {} } } @@ -34,25 +34,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate { 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:. } diff --git a/examples/Sample/Sample/FileBrowser.swift b/examples/Sample/Sample/FileBrowser.swift index b1915e3..b85e65c 100644 --- a/examples/Sample/Sample/FileBrowser.swift +++ b/examples/Sample/Sample/FileBrowser.swift @@ -17,9 +17,9 @@ class FileBrowser: UIViewController, UITableViewDataSource, UITableViewDelegate @IBOutlet weak var zipButton: UIBarButtonItem! @IBOutlet weak var unzipButton: UIBarButtonItem! - let fileManager = NSFileManager.defaultManager() + let fileManager = FileManager.default - var path: NSURL? { + var path: URL? { didSet { updateFiles() } @@ -34,7 +34,7 @@ class FileBrowser: UIViewController, UITableViewDataSource, UITableViewDelegate override func viewDidLoad() { if self.path == nil { - let documentsUrl = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL + let documentsUrl = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0] as URL self.path = documentsUrl } updateSelection() @@ -47,7 +47,7 @@ class FileBrowser: UIViewController, UITableViewDataSource, UITableViewDelegate var tempFiles = [String]() do { self.title = filePath.lastPathComponent - tempFiles = try self.fileManager.contentsOfDirectoryAtPath(filePath.path!) + tempFiles = try self.fileManager.contentsOfDirectory(atPath: filePath.path) } catch { if path == "/System" { tempFiles = ["Library"] @@ -62,50 +62,50 @@ class FileBrowser: UIViewController, UITableViewDataSource, UITableViewDelegate tempFiles = ["lib", "libexec", "bin"] } } - self.files = tempFiles.sort(){$0 < $1} + self.files = tempFiles.sorted(){$0 < $1} tableView.reloadData() } } //MARK: UITableView Data Source and Delegate - func numberOfSectionsInTableView(tableView: UITableView) -> Int { + func numberOfSections(in tableView: UITableView) -> Int { return 1 } - func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return files.count } - func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellIdentifier = "FileCell" - var cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellIdentifier) - if let reuseCell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) { + var cell = UITableViewCell(style: .subtitle, reuseIdentifier: cellIdentifier) + if let reuseCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) { cell = reuseCell } guard let path = path else { return cell } - cell.selectionStyle = .None - let filePath = files[indexPath.row] - let newPath = path.URLByAppendingPathComponent(filePath).path! + cell.selectionStyle = .none + let filePath = files[(indexPath as NSIndexPath).row] + let newPath = path.appendingPathComponent(filePath).path var isDirectory: ObjCBool = false - fileManager.fileExistsAtPath(newPath, isDirectory: &isDirectory) - cell.textLabel?.text = files[indexPath.row] + fileManager.fileExists(atPath: newPath, isDirectory: &isDirectory) + cell.textLabel?.text = files[(indexPath as NSIndexPath).row] if isDirectory { cell.imageView?.image = UIImage(named: "Folder") } else { cell.imageView?.image = UIImage(named: "File") } - cell.backgroundColor = (selectedFiles.contains(filePath)) ? UIColor(white: 0.9, alpha: 1.0):UIColor.whiteColor() + cell.backgroundColor = (selectedFiles.contains(filePath)) ? UIColor(white: 0.9, alpha: 1.0):UIColor.white return cell } - func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { - let filePath = files[indexPath.row] - if let index = selectedFiles.indexOf(filePath) where selectedFiles.contains(filePath) { - selectedFiles.removeAtIndex(index) + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + let filePath = files[(indexPath as NSIndexPath).row] + if let index = selectedFiles.index(of: filePath) , selectedFiles.contains(filePath) { + selectedFiles.remove(at: index) } else { selectedFiles.append(filePath) @@ -117,27 +117,27 @@ class FileBrowser: UIViewController, UITableViewDataSource, UITableViewDelegate tableView.reloadData() selectionCounter.title = "\(selectedFiles.count) Selected" - zipButton.enabled = (selectedFiles.count > 0) + zipButton.isEnabled = (selectedFiles.count > 0) if (selectedFiles.count == 1) { let filePath = selectedFiles.first - let pathExtension = path!.URLByAppendingPathComponent(filePath!).pathExtension + let pathExtension = path!.appendingPathComponent(filePath!).pathExtension if pathExtension == "zip" { - unzipButton.enabled = true + unzipButton.isEnabled = true } else { - unzipButton.enabled = false + unzipButton.isEnabled = false } } else { - unzipButton.enabled = false + unzipButton.isEnabled = false } } //MARK: Actions - @IBAction func unzipSelection(sender: AnyObject) { + @IBAction func unzipSelection(_ sender: AnyObject) { let filePath = selectedFiles.first - let pathURL = path!.URLByAppendingPathComponent(filePath!) + let pathURL = path!.appendingPathComponent(filePath!) do { try Zip.quickUnzipFile(pathURL) self.selectedFiles.removeAll() @@ -148,10 +148,10 @@ class FileBrowser: UIViewController, UITableViewDataSource, UITableViewDelegate } } - @IBAction func zipSelection(sender: AnyObject) { - var urlPaths = [NSURL]() + @IBAction func zipSelection(_ sender: AnyObject) { + var urlPaths = [URL]() for filePath in selectedFiles { - urlPaths.append(path!.URLByAppendingPathComponent(filePath)) + urlPaths.append(path!.appendingPathComponent(filePath)) } do { try Zip.quickZipFiles(urlPaths, fileName: "Archive") diff --git a/examples/Sample/SampleTests/SampleTests.swift b/examples/Sample/SampleTests/SampleTests.swift index e293980..38fa1d6 100644 --- a/examples/Sample/SampleTests/SampleTests.swift +++ b/examples/Sample/SampleTests/SampleTests.swift @@ -28,7 +28,7 @@ class SampleTests: 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. } } -- 2.26.2