2016-05-25 9 views
1

ストリームイベントを取得した後にbuttonを更新できないようである点を除いて、以下のコードが機能するようです。Swift:ストリームをリッスンするときにUIを更新する方法

私はdelegatedispatch_asyncの両方を試しましたが、いずれもそのようなことはしていないようです。

ここではどのような魔法がありますか?

import UIKit 

class TestViewController: UIViewController { 

    @IBOutlet weak var connectButton: UIBarButtonItem! 
    @IBOutlet weak var stopButton: UIBarButtonItem! 

    let socket = Socket() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    @IBAction func connectServer(sender: UIBarButtonItem) { 
     socket.connect("10.0.0.1", port: 12345) 
    } 

    func setStopButtonState(state: String) { 
     switch state { 
     case "go": 
      stopButton?.title = "STOP" 
      break 
     case "stop": 
      stopButton?.title = "Start" 
      break 
     default: 
      break 
     } 
    } 
} 

class Socket: NSObject, NSStreamDelegate { 
    var inputStream: NSInputStream? 
    var outputStream: NSOutputStream? 

    func connect(host: String, port: Int) { 
     NSStream.getStreamsToHostWithName(host, port: port, inputStream: &inputStream, outputStream: &outputStream) 
     if inputStream != nil && outputStream != nil { 
      inputStream!.delegate = self 
      outputStream!.delegate = self 
      inputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode) 
      outputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode) 
      inputStream!.open() 
      outputStream!.open() 
     } 
    } 

    func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) { 
     if aStream === inputStream { 
      switch eventCode { 
      case NSStreamEvent.HasBytesAvailable: 
       var inputBuffer = Array<UInt8>(count:2048, repeatedValue: 0) 
       inputStream?.read(&inputBuffer, maxLength: 2048) 
       TestViewController().setStopButtonState(String(bytes: inputBuffer, encoding: NSUTF8StringEncoding)!) 
       break 
      default: 
       break 
      } 
     } 
    } 
} 

答えて

0

ソケットはコールバックを持っている、とストリームがバイトしているときにそれを呼びましょう:

import UIKit 

class TestViewController: UIViewController { 

@IBOutlet weak var connectButton: UIBarButtonItem! 
@IBOutlet weak var stopButton: UIBarButtonItem! 

let socket = Socket() 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

@IBAction func connectServer(sender: UIBarButtonItem) { 
    socket.connect("10.0.0.1", port: 12345) 
    socket.changeWhenHasBytesAvailable = { str in 
     // do change for the string 
    } 
} 

func setStopButtonState(state: String) { 
    switch state { 
    case "go": 
     stopButton?.title = "STOP" 
     break 
    case "stop": 
     stopButton?.title = "Start" 
     break 
    default: 
     break 
    } 
} 
} 

class Socket: NSObject, NSStreamDelegate { 
var inputStream: NSInputStream? 
var outputStream: NSOutputStream? 
var changeWhenHasBytesAvailable: ((String)->())? 

func connect(host: String, port: Int) { 
    NSStream.getStreamsToHostWithName(host, port: port, inputStream: &inputStream, outputStream: &outputStream) 
    if inputStream != nil && outputStream != nil { 
     inputStream!.delegate = self 
     outputStream!.delegate = self 
     inputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode) 
     outputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode) 
     inputStream!.open() 
     outputStream!.open() 
    } 
} 

func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) { 
    if aStream === inputStream { 
     switch eventCode { 
     case NSStreamEvent.HasBytesAvailable: 
      var inputBuffer = Array<UInt8>(count:2048, repeatedValue: 0) 
      inputStream?.read(&inputBuffer, maxLength: 2048) 
      //call it in main queue 
      dispatch_async(dispatch_get_main_queue(), { 
       self.changeWhenHasBytesAvailable?(String(bytes: inputBuffer, encoding: NSUTF8StringEncoding)!) 
      }) 
      break 
     default: 
      break 
     } 
    } 
} 
} 
+0

これは素晴らしい仕事、ありがとうございました! – Jason

関連する問題