title: How to use SCNetworkReachability in Swift
body: <p>I'm trying to convert <a href="http://www.chrisdanielson.com/2009/07/22/iphone-network-connectivity-test-example/#comments" rel="noreferrer">this</a> code snippet to Swift. I'm struggling on getting off the ground due to some difficulties.</p>
  
  <pre><code>- (BOOL) connectedToNetwork
  {
      // Create zero addy
      struct sockaddr_in zeroAddress;
      bzero(&amp;zeroAddress, sizeof(zeroAddress));
      zeroAddress.sin_len = sizeof(zeroAddress);
      zeroAddress.sin_family = AF_INET;
  
      // Recover reachability flags
      SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&amp;zeroAddress);
      SCNetworkReachabilityFlags flags;
  
      BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &amp;flags);
      CFRelease(defaultRouteReachability);
  
      if (!didRetrieveFlags)
      {
          return NO;
      }
  
      BOOL isReachable = flags &amp; kSCNetworkFlagsReachable;
      BOOL needsConnection = flags &amp; kSCNetworkFlagsConnectionRequired;
  
      return (isReachable &amp;&amp; !needsConnection) ? YES : NO;
  }
  </code></pre>
  
  <p>The first and the main issue I'm having is on how to define and work with C structs. In the first line (<code>struct sockaddr_in zeroAddress;</code>) of the above code, I think they're defining a instance called <code>zeroAddress</code> from the struct sockaddr_in(?), I assume. I tried declaring a <code>var</code> like this.</p>
  
  <pre><code>var zeroAddress = sockaddr_in()
  </code></pre>
  
  <p>But I get the error <strong>Missing argument for parameter 'sin_len' in call</strong> which is understandable because that struct takes a number of arguments. So I tried again.</p>
  
  <pre><code>var zeroAddress = sockaddr_in(sin_len: sizeof(zeroAddress), sin_family: AF_INET, sin_port: nil, sin_addr: nil, sin_zero: nil)
  </code></pre>
  
  <p>As expected I get some other error <strong>Variable used within its own initial value</strong>. I understand the cause of that error too. In C, they declare the instance first and then fill up the parameters. Its not possible in Swift as far as I know. So I'm truly lost at this point on what to do.</p>
  
  <p>I read Apple's official <a href="https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_13" rel="noreferrer">document</a> on interacting with C APIs in Swift but it has no examples in working with structs. </p>
  
  <p>Can anyone please help me out here? I'd really appreciate it.</p>
  
  <p>Thank you.</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong> Thanks to Martin I was able to get past the initial problem. But still Swift ain't making it easier for me. I'm getting multiple new errors.</p>
  
  <pre><code>func connectedToNetwork() -&gt; Bool {
  
      var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
      zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
      zeroAddress.sin_family = sa_family_t(AF_INET)
  
      var defaultRouteReachability: SCNetworkReachabilityRef = SCNetworkReachabilityCreateWithAddress(UnsafePointer&lt;Void&gt;, UnsafePointer&lt;zeroAddress&gt;) // 'zeroAddress' is not a type
      var flags = SCNetworkReachabilityFlags()
  
      let didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, UnsafeMutablePointer&lt;flags&gt;) // 'flags' is not a type
      defaultRouteReachability.dealloc(1) // 'SCNetworkReachabilityRef' does not have a member named 'dealloc'
  
      if didRetrieveFlags == false {
          return false
      }
  
      let isReachable: Bool = flags &amp; kSCNetworkFlagsReachable // Cannot invoke '&amp;' with an argument list of type '(@lvalue UInt32, Int)'
      let needsConnection: Bool = flags &amp; kSCNetworkFlagsConnectionRequired // Cannot invoke '&amp;' with an argument list of type '(@lvalue UInt32, Int)'
  
      return (isReachable &amp;&amp; !needsConnection) ? true : false
  }
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT 1:</strong>  Okay I changed this line to this,</p>
  
  <pre><code>var defaultRouteReachability: SCNetworkReachabilityRef = SCNetworkReachabilityCreateWithAddress(UnsafePointer&lt;Void&gt;(), &amp;zeroAddress)
  </code></pre>
  
  <p>The new error I'm getting at this line is <strong>'UnsafePointer' is not convertible to 'CFAllocator'</strong>. How to you pass <code>NULL</code> in Swift?</p>
  
  <p>Also I changed this line and the error is gone now.</p>
  
  <pre><code>let didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &amp;flags)
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT 2:</strong> I passed <code>nil</code> in this line after seeing <a href="https://stackoverflow.com/questions/24065774/calling-a-function-with-null-as-parameter">this</a> question. But that answer contradicts with the answer <a href="https://stackoverflow.com/a/24043722/1077789">here</a>. It says there is no equivalent to <code>NULL</code> in Swift.</p>
  
  <pre><code>var defaultRouteReachability: SCNetworkReachabilityRef = SCNetworkReachabilityCreateWithAddress(nil, &amp;zeroAddress)
  </code></pre>
  
  <p>Anyway I get a new error saying <strong>'sockaddr_in' is not identical to 'sockaddr'</strong> at the above line.</p>