Basically the issue is going to be getting your data correctly converted to a pointer to get it to NSOutputStream.write
. The following extension should do what you need:
extension NSOutputStream {
enum WriteErrors : ErrorType {
case UTF8ConversionFailed
}
func write(string:String) throws -> Int {
guard let data = string.dataUsingEncoding(NSUTF8StringEncoding) else {
throw WriteErrors.UTF8ConversionFailed
}
return write(UnsafePointer(data.bytes), maxLength: data.length)
}
func write(var i:Int32) throws -> Int {
return withUnsafePointer(&i) {
self.write(UnsafePointer($0), maxLength: sizeof(Int32))
}
}
}
Note that writing raw binary data to a file is ill-advised, you should be standardizing the byte order first, but I leave that as an exercise for the reader.