title: Receiving Data from NSInputStream in Swift
body: <p>I try to send and receive data with NSOutputStream and NSInputStream in Swift. The sending of data is working well, but i have some questions about the receiving.</p>
  
  <p>I found a solution with handling the NSStreamEvent, which i have tried.</p>
  
  <p>First of all my function for initializing the connection:</p>
  
  <pre><code>func initNetworkCommunication(){  
      var host : CFString = "127.0.0.1"
      var port : UInt32 = 7001
      var readstream : Unmanaged&lt;CFReadStream&gt;?
      var writestream : Unmanaged&lt;CFWriteStream&gt;?
      CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &amp;readstream, &amp;writestream)
  
      inputstream = readstream!.takeRetainedValue()
      outputstream = writestream!.takeRetainedValue()
  
      inputstream.delegate = self
      outputstream.delegate = self
  
  
      inputstream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
      outputstream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
  
      inputstream.open()
      outputstream.open()   
  }
  </code></pre>
  
  <p>This part is working. I have set the delegates to self, so i should be able to handle the NSStreamEvents in this class. </p>
  
  <pre><code>func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
      switch (eventCode){
      case NSStreamEvent.OpenCompleted:
          NSLog("Stream opened")
          break
      case NSStreamEvent.HasBytesAvailable:
          NSLog("HasBytesAvailable")
          break
      case NSStreamEvent.ErrorOccurred:
           NSLog("ErrorOccurred")
          break
      case NSStreamEvent.EndEncountered:
          NSLog("EndEncountered")
          break
      default:
          NSLog("unknown.")
      }
  }
  </code></pre>
  
  <p>In my understanding everytime when some data arrives, it should print "HasBytesAvaible", but it printed "unknown." everytime. So i played around a bit and it worked with this:</p>
  
  <pre><code>func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
  
      var buffer = [UInt8](count: 4096, repeatedValue: 0)
      while (inputstream.hasBytesAvailable){
          let result: Int = inputstream.read(&amp;buffer, maxLength: buffer.count)
      }
  
      switch (eventCode){
      case NSStreamEvent.OpenCompleted:
          NSLog("Stream opened")
          break
      case NSStreamEvent.HasBytesAvailable:
          NSLog("HasBytesAvailable")
          var output = NSString(bytes: &amp;buffer, length: buffer.count, encoding: NSUTF8StringEncoding)
          NSLog("output: %@", output)
          receiveMessage(output) //only adds the message to an array
          break
      case NSStreamEvent.ErrorOccurred:
          NSLog("ErrorOccurred")
          break
      case NSStreamEvent.EndEncountered:
            NSLog("EndEncountered")
          break
      default:
          NSLog("unknown.")
      }
  }
  </code></pre>
  
  <p>It's working this way, but i wanted to ask you whether this is the right way ? What is the best practice at this point ? </p>
  
  <p><strong>UPDATE</strong> 
  I worked on it again a few weeks ago and figured out my mistakes. So here is the working code.</p>
  
  <pre><code>func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
          switch (eventCode){
      case NSStreamEvent.ErrorOccurred:
          NSLog("ErrorOccurred")
          break
      case NSStreamEvent.EndEncountered:
          NSLog("EndEncountered")
          break
      case NSStreamEvent.None:
          NSLog("None")
          break
      case NSStreamEvent.HasBytesAvailable:
          NSLog("HasBytesAvaible")
          var buffer = [UInt8](count: 4096, repeatedValue: 0)
          if ( aStream == inputstream){
  
              while (inputstream.hasBytesAvailable){
                  var len = inputstream.read(&amp;buffer, maxLength: buffer.count) 
                  if(len &gt; 0){
                      var output = NSString(bytes: &amp;buffer, length: buffer.count, encoding: NSUTF8StringEncoding) 
                      if (output != ""){
                          NSLog("server said: %@", output!)
                      }
                  }
              } 
          }
          break
      case NSStreamEvent.allZeros:
          NSLog("allZeros")
          break
      case NSStreamEvent.OpenCompleted:
          NSLog("OpenCompleted")
          break
      case NSStreamEvent.HasSpaceAvailable:
          NSLog("HasSpaceAvailable")
          break
    }
  </code></pre>