浏览代码

Make throwing inits just failable

Ashley Mills 9 年之前
父节点
当前提交
494d0a4d2e
共有 2 个文件被更改,包括 20 次插入26 次删除
  1. 18 18
      Reachability/Reachability.swift
  2. 2 8
      ReachabilitySample/ViewController.swift

+ 18 - 18
Reachability/Reachability.swift

@@ -51,7 +51,7 @@ func callback(reachability:SCNetworkReachability, flags: SCNetworkReachabilityFl
     }
 }
 
-public class Reachability: NSObject {
+public class Reachability {
 
     public typealias NetworkReachable = (Reachability) -> ()
     public typealias NetworkUnreachable = (Reachability) -> ()
@@ -110,25 +110,14 @@ public class Reachability: NSObject {
         self.reachabilityRef = reachabilityRef
     }
     
-    deinit {
-        stopNotifier()
-
-        reachabilityRef = nil
-        whenReachable = nil
-        whenUnreachable = nil
-    }
-}
-
-public extension Reachability {
-    
-    convenience init(hostname: String) throws {
+    public convenience init?(hostname: String) {
         
-        guard let ref = SCNetworkReachabilityCreateWithName(nil, hostname) else { throw ReachabilityError.FailedToCreateWithHostname(hostname) }
+        guard let ref = SCNetworkReachabilityCreateWithName(nil, hostname) else { return nil }
         
         self.init(reachabilityRef: ref)
     }
     
-    class func reachabilityForInternetConnection() throws -> Reachability {
+    public convenience init?() {
         
         var zeroAddress = sockaddr_in()
         zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
@@ -136,11 +125,22 @@ public extension Reachability {
         
         guard let ref = withUnsafePointer(&zeroAddress, {
             SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
-        }) else { throw ReachabilityError.FailedToCreateWithAddress(zeroAddress) }
+        }) else { return nil }
         
-        return Reachability(reachabilityRef: ref)
+        self.init(reachabilityRef: ref)
     }
     
+    deinit {
+        stopNotifier()
+
+        reachabilityRef = nil
+        whenReachable = nil
+        whenUnreachable = nil
+    }
+}
+
+public extension Reachability {
+    
     // MARK: - *** Notifier methods ***
     func startNotifier() throws {
         
@@ -211,7 +211,7 @@ public extension Reachability {
         return !isOnWWANFlagSet
     }
     
-    override var description: String {
+    var description: String {
         
         let W = isRunningOnDevice ? (isOnWWANFlagSet ? "W" : "-") : "X"
         let R = isReachableFlagSet ? "R" : "-"

+ 2 - 8
ReachabilitySample/ViewController.swift

@@ -44,14 +44,8 @@ class ViewController: UIViewController {
         
         print("--- set up with host name: \(hostNameLabel.text!)")
 
-        do {
-            let reachability = try hostName == nil ? Reachability.reachabilityForInternetConnection() : Reachability(hostname: hostName!)
-            self.reachability = reachability
-        } catch ReachabilityError.FailedToCreateWithAddress(let address) {
-            networkStatus.textColor = .red
-            networkStatus.text = "Unable to create\nReachability with address:\n\(address)"
-            return
-        } catch {}
+        let reachability = hostName == nil ? Reachability() : Reachability(hostname: hostName!)
+        self.reachability = reachability
         
         if useClosures {
             reachability?.whenReachable = { reachability in