Writing a String to an NSOutputStream in Swift
Asked Answered
C

3

12

I'm trying to write a String to an NSOutputStream in Swift. Writing Strings that way with Objective C usually works by passing it as NSData

NSData *data = [[NSData alloc] initWithData:[mystring dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];

This does not work with swift

var data: NSData = mystring.dataUsingEncoding(NSUTF8StringEncoding)!
outputStream.write(data, maxLength: data.length)

this yields the error

'NSData' is not convertible to 'UnsafePointer'

for the line that writes the data to the stream.

How would you write a String to an NSOutputStream in Swift?

Craunch answered 13/10, 2014 at 1:6 Comment(0)
R
15

There are two issues here. The first is that you're passing data to outputStream.write() and not data.bytes (like you passed [data bytes] in your Objective-C code). The second issue is that data.bytes returns an UnsafePointer<Void>, but NSOutputStream.write() takes an UnsafePointer<UInt8>. Luckily, UnsafePointer has a way to convert between types:

/// Convert from a UnsafePointer of a different type.
///
/// This is a fundamentally unsafe conversion.
init<U>(_ from: UnsafePointer<U>)

Putting those things together makes your code look something like this:

let data: NSData = mystring.dataUsingEncoding(NSUTF8StringEncoding)!
outputStream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length)
Randle answered 13/10, 2014 at 4:37 Comment(0)
F
27

In recent Swift it should be even easier and NSData is no longer needed.

let s = "String to encode"
let encodedDataArray = [UInt8](s.utf8)
outputstream.write(encodedDataArray, maxLength: encodedDataArray.count)

Arrays can be accessed as buffers of the correct their type (see withUnsafeBufferPointer). I think the array is necessary because the utf8 view is not actually instantiated as a full array but just a view into the original string.

In production code you should check the return value of the write to the output stream and depending on your scenario check there is space before the write but the focus of this answer is the encoding of the Swift String so that it can be written.

Fissile answered 4/9, 2015 at 11:35 Comment(0)
R
15

There are two issues here. The first is that you're passing data to outputStream.write() and not data.bytes (like you passed [data bytes] in your Objective-C code). The second issue is that data.bytes returns an UnsafePointer<Void>, but NSOutputStream.write() takes an UnsafePointer<UInt8>. Luckily, UnsafePointer has a way to convert between types:

/// Convert from a UnsafePointer of a different type.
///
/// This is a fundamentally unsafe conversion.
init<U>(_ from: UnsafePointer<U>)

Putting those things together makes your code look something like this:

let data: NSData = mystring.dataUsingEncoding(NSUTF8StringEncoding)!
outputStream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length)
Randle answered 13/10, 2014 at 4:37 Comment(0)
A
8

Hope this example helps out. It writes a string to document directory. Note that outputStream is tested using Swift 1.1 with failable initializer.

var myString = "Hello world!"
var docPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
var path = docPath.stringByAppendingPathComponent("doc")
var outputStream = NSOutputStream(toFileAtPath: path, append: false)
var data: NSData = myString.dataUsingEncoding(NSUTF8StringEncoding)!
var buffer = [UInt8](count:data.length, repeatedValue:0)
data.getBytes(&buffer)
outputStream?.open()
outputStream?.write(&buffer, maxLength: data.length)
outputStream?.close()

Regards

Anteater answered 13/10, 2014 at 2:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.