Receiving Data from NSInputStream in Swift

不羁的心 提交于 2019-12-02 17:14:50

You're missing the event hasSpaceAvailable, which I expect is occurring when it says "unknown". It's telling you that it is ready to receive more data.

Generally, I avoid using default in switch statements for enums, since the compiler will tell you when you've missed something.

I am using the code written by hoedding, and noticed a mistake. The line:

var output = NSString(bytes: &buffer, length: buffer.count, encoding: NSUTF8StringEncoding)

should be replaced by:

var output = NSString(bytes: &buffer, length: len, encoding: NSUTF8StringEncoding)

You have to get in the output var only the number of characters returned by the server, and not the full length of the buffer, or you will get garbage from previous requests.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!