Swift streams and sockets -- help needed

287 views
Skip to first unread message

Jerzy Głowacki

unread,
Jul 26, 2014, 11:32:51 AM7/26/14
to swift-l...@googlegroups.com
Hello,

Could anybody please help solving this problem: Swift streams and sockets?

Thanks!

Tom Lieber

unread,
Jul 26, 2014, 12:48:41 PM7/26/14
to swift-l...@googlegroups.com
You can pop this into a Playground to learn about NSInputStream:

import Cocoa

// create some data to read
let data: NSData = "Howdy,
pardner.".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion:
true)
data.length

// create a stream that reads the data above
let stream: NSInputStream = NSInputStream(data: data)

// begin reading
stream.open()
var buffer = [UInt8](count: 8, repeatedValue: 0)
while stream.hasBytesAvailable {
    let result: Int = stream.read(&buffer, maxLength: buffer.count)
    result // the number of bytes read is here
    buffer // the data read is in here
}


--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/2e51890f-91c0-4527-b5ec-266a3ea4d15e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
Tom Lieber
http://AllTom.com/
http://infinite-sketchpad.com/

Jerzy Głowacki

unread,
Jul 26, 2014, 1:55:25 PM7/26/14
to swift-l...@googlegroups.com, all...@gmail.com
Thanks a lot! Your answer has been posted under the original question.
Reply all
Reply to author
Forward
0 new messages