Does swift have a protocol for writing a stream of bytes?
Asked Answered
S

1

8

I can't find anything in the Swift book about io. Is there any generic protocol similar to Java's OutputStream or Go's Writer interface for writing a stream of bytes? If you are writing a class that returns a stream, do you need to write your own protocol or use an Objective C protocol?

To be clear am asking for a Swift native interface for this not because I avoiding using Objective C or Cocoa, but for a description of the expected behavior for Swift to Swift code going forward.

Sherly answered 12/6, 2014 at 12:28 Comment(5)
developer.apple.com/library/ios/documentation/Cocoa/Conceptual/…Amadoamador
Seems odd you don't have something this basic as part of the standard library. The NSStream really isn't how you would do it if you wrote it in swift.Sherly
I agree Swift is lacking in this area at the moment but it is designed to work with Cocoa frameworks. is there something specific you are trying to do?Amadoamador
@tony Very curious - why would you not use NSStream? I don't think that just because Swift is around doesn't mean Cocoa is going away. I'd say it's a very legitimate way to work with streams in Swift.Camenae
@Camenae No I will use NSStream and of course it is legitimate way to do it. It just doesn't seems very idiomatic. I was expecting to need to use cocoa regularly for particular uses but there really no examples of the swift way to do io. How do you return errors without nserror, when to use optionals, etc. Even if barely anything implements the swift writer protocol, if I writing something new, I want to use that instead of the Obj C way. It really about implementation vs protocol. I could be writing both the reader and the writer, and want to know the native way to do it.Sherly
A
15

This is something the Swift docs are quiet about and I wanted to know more about so I looked into it.

There is a protocol, it's called Streamable:

protocol Streamable {
    func writeTo<Target : OutputStream>(inout target: Target)
}

OutputStream:

protocol OutputStream {
    func write(string: String)
}

write allows an object to be written to.

String conforms to both, making it easy to write to and from:

var target = String()
"this is a message".writeTo(&target)
println(target)
// this is a message

Writing to a file:

var msg = "this will be written to an output file"
msg.writeToFile("output.txt", atomically: false, encoding: NSUTF8StringEncoding, error: nil)
// creates 'output.txt' in the same folder as the executable

There is also writeToUrl.

I assume those functions are all built on top of Cocoa streams, which have similar functionality:

var os = NSOutputStream(toFileAtPath: "output.txt", append: true)
os.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)

var msg = "a truly remarkable message"
var ptr:CConstPointer<UInt8> = msg.nulTerminatedUTF8

os.open()
os.write(ptr, maxLength: msg.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
os.close()
Amadoamador answered 12/6, 2014 at 13:2 Comment(2)
Nice find. It seems like they are still working it which is reassuring.Sherly
Note that the protocols have been renamed to TextOutputStreamable and TextOutputStream in Swift 5 (and 4 too I think)Hoad

© 2022 - 2024 — McMap. All rights reserved.