in the Ray Wenderlich tutorial on sockets , to read bytes from the input stream in Objective-C, we did
uint8_t buffer[1024]; int len; while ([inputStream hasBytesAvailable]) { len = [inputStream read:buffer maxLength:sizeof(buffer)]; if (len > 0) { NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; if (nil != output) { NSLog(@"server said: %@", output); }
In Swift, I tried the following without much success
if (stream == inputStream) { // var buffer = Array<UInt8>(count: 1024, repeatedValue: 0) var buffer : UnsafeMutablePointer<UInt8> var len : Bool while (inputStream?.hasBytesAvailable == true) { len = inputStream?.getBuffer(buffer, length: sizeofValue(buffer)) if (len) { var output = String(NSString(bytes: buffer, length: sizeofValue(buffer), encoding: NSASCIIStringEncoding)) } } }
source share