title: Having trouble using variables to represent Host and Port when initializing NWConnection in Swift
body: <p>I am trying to write a macOS application that connects to a radio scanner on a network connection via a UDP socket. The app sends commands to the scanner for remote control.  I can get the app to work fine when I hard code the NWConnection initiation but when I put a variable that represents the port or host I get an Xcode error.</p>
  <p>I want the user to be able to input their specific Host and Port address therefore I set up textFields for the user to input these values. Then I wanted to retrieve the values and enter them into the Host and Port NWConnection fields when initializing the connection.</p>
  <p>Here is a code snippet:</p>
  <pre><code>func makeConnection(){
          var myHost = &quot;192.168.7.239&quot;
          var myPort = 50536 as UInt16
          
          myConnection = NWConnection(host: myHost, port: myPort, using: .udp)
          myConnection?.stateUpdateHandler = { (newState) in
              switch (newState) {
              case .ready:
                  print(&quot;ready&quot;)
                  self.send()
                  self.receive()
              case .setup:
                  print(&quot;setup&quot;)
              case .cancelled:
                  print(&quot;cancelled&quot;)
              case .preparing:
                  print(&quot;preparing&quot;)
              default:
                  print(&quot;waiting or failed&quot;)
                  
              }
          }  
  </code></pre>
  <p>In this case, if I replace myHost with &quot;192.168.7.239&quot; and I replace myPort with 50536 then Xcode is happy and everything works.  However as soon as I put a variable in the host and port fields I get this complaint from Xcode.</p>
  <p>&quot;Type of expression is ambiguous without more context&quot; and the build fails.</p>
  <p>Why is this happening and how can I make an NWConnection without hard coding a Host or Port? what good is it is you can't get the port and host from the user and use those values to make the connection.</p>
  <p>Any help would be greatly appreciated.</p>