Ashley Mills 9 жил өмнө
parent
commit
402bf797da

+ 3 - 2
Reachability.xcodeproj/project.pbxproj

@@ -422,6 +422,7 @@
 					};
 					00C54B1E1C09CF67001C3F12 = {
 						CreatedOnToolsVersion = 7.1.1;
+						LastSwiftMigration = 0800;
 					};
 					57A45A001C197BE800384AE4 = {
 						CreatedOnToolsVersion = 7.1.1;
@@ -829,6 +830,7 @@
 				ONLY_ACTIVE_ARCH = YES;
 				SDKROOT = iphoneos;
 				SWIFT_OPTIMIZATION_LEVEL = "-Onone";
+				SWIFT_VERSION = 3.0;
 				TARGETED_DEVICE_FAMILY = "1,2";
 				VERSIONING_SYSTEM = "apple-generic";
 				VERSION_INFO_PREFIX = "";
@@ -871,6 +873,7 @@
 				IPHONEOS_DEPLOYMENT_TARGET = 8.0;
 				MTL_ENABLE_DEBUG_INFO = NO;
 				SDKROOT = iphoneos;
+				SWIFT_VERSION = 3.0;
 				TARGETED_DEVICE_FAMILY = "1,2";
 				VALIDATE_PRODUCT = YES;
 				VERSIONING_SYSTEM = "apple-generic";
@@ -932,7 +935,6 @@
 				LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
 				PRODUCT_BUNDLE_IDENTIFIER = uk.co.joylordsystems.ReachabilitySample;
 				PRODUCT_NAME = "$(TARGET_NAME)";
-				SWIFT_VERSION = 3.0;
 			};
 			name = Debug;
 		};
@@ -946,7 +948,6 @@
 				PRODUCT_BUNDLE_IDENTIFIER = uk.co.joylordsystems.ReachabilitySample;
 				PRODUCT_NAME = "$(TARGET_NAME)";
 				SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
-				SWIFT_VERSION = 3.0;
 			};
 			name = Release;
 		};

+ 16 - 16
ReachabilityMacSample/ViewController.swift

@@ -18,22 +18,22 @@ class ViewController: NSViewController {
   override func viewDidLoad() {
     super.viewDidLoad()
     view.wantsLayer = true
-    view.layer?.backgroundColor = NSColor.whiteColor().CGColor
+    view.layer?.backgroundColor = .white.cgColor
 
     // Start reachability without a hostname intially
     setupReachability(useHostName: false, useClosures: true)
     startNotifier()
 
     // After 5 seconds, stop and re-start reachability, this time using a hostname
-    let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
-    dispatch_after(dispatchTime, dispatch_get_main_queue()) {
+    let dispatchTime = DispatchTime.now() + Double(Int64(UInt64(5) * NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
+    DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
       self.stopNotifier()
       self.setupReachability(useHostName: true, useClosures: true)
       self.startNotifier()
     }
   }
 
-  func setupReachability(useHostName useHostName: Bool, useClosures: Bool) {
+  func setupReachability(useHostName: Bool, useClosures: Bool) {
     let hostName = "google.com"
     hostNameLabel.stringValue = useHostName ? hostName : "No host name"
 
@@ -43,7 +43,7 @@ class ViewController: NSViewController {
       let reachability = try useHostName ? Reachability(hostname: hostName) : Reachability.reachabilityForInternetConnection()
       self.reachability = reachability
     } catch ReachabilityError.FailedToCreateWithAddress(let address) {
-      networkStatus.textColor = NSColor.redColor()
+      networkStatus.textColor = .red
       networkStatus.stringValue = "Unable to create\nReachability with address:\n\(address)"
       return
     } catch {}
@@ -56,7 +56,7 @@ class ViewController: NSViewController {
         self.updateLabelColourWhenNotReachable(reachability)
       }
     } else {
-      NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
+      NotificationCenter.default.addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
     }
   }
 
@@ -65,7 +65,7 @@ class ViewController: NSViewController {
     do {
       try reachability?.startNotifier()
     } catch {
-      networkStatus.textColor = NSColor.redColor()
+      networkStatus.textColor = .red
       networkStatus.stringValue = "Unable to start\nnotifier"
       return
     }
@@ -74,33 +74,33 @@ class ViewController: NSViewController {
   func stopNotifier() {
     print("--- stop notifier")
     reachability?.stopNotifier()
-    NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
+    NotificationCenter.default.removeObserver(self, name: ReachabilityChangedNotification, object: nil)
     reachability = nil
   }
 
-  func updateLabelColourWhenReachable(reachability: Reachability) {
+  func updateLabelColourWhenReachable(_ reachability: Reachability) {
     print("\(reachability.description) - \(reachability.currentReachabilityString)")
-    if reachability.isReachableViaWiFi() {
-      self.networkStatus.textColor = NSColor.greenColor()
+    if reachability.isReachableViaWiFi {
+      self.networkStatus.textColor = .green
     } else {
-      self.networkStatus.textColor = NSColor.blueColor()
+      self.networkStatus.textColor = .blue
     }
 
     self.networkStatus.stringValue = reachability.currentReachabilityString
   }
 
-  func updateLabelColourWhenNotReachable(reachability: Reachability) {
+  func updateLabelColourWhenNotReachable(_ reachability: Reachability) {
     print("\(reachability.description) - \(reachability.currentReachabilityString)")
 
-    self.networkStatus.textColor = NSColor.redColor()
+    self.networkStatus.textColor = .red
 
     self.networkStatus.stringValue = reachability.currentReachabilityString
   }
 
-  func reachabilityChanged(note: NSNotification) {
+  func reachabilityChanged(_ note: Notification) {
     let reachability = note.object as! Reachability
 
-    if reachability.isReachable() {
+    if reachability.isReachable {
       updateLabelColourWhenReachable(reachability)
     } else {
       updateLabelColourWhenNotReachable(reachability)