Skip to content

Instantly share code, notes, and snippets.

@swishy
Created March 5, 2021 10:20
Show Gist options
  • Save swishy/22c2c178ea8a42bc9ddc29a1950f226c to your computer and use it in GitHub Desktop.
Save swishy/22c2c178ea8a42bc9ddc29a1950f226c to your computer and use it in GitHub Desktop.
swift OutputStream delegate issue
import Foundation
class TestClass: NSObject, StreamDelegate {
func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
print("FOO")
}
func main() {
let profile = "<amin:command name='mkdir' xmlns:amin='http://projectamin.org/ns/'>\n\t<amin:flag name='m'>0755</amin:flag>\n\t<amin:param name=\"target\">/tmp/test_ashell</amin:param>\n</amin:command>"
// Testing delegate
let testStream = OutputStream.toMemory()
testStream.schedule(in: RunLoop.current, forMode: RunLoop.Mode.default)
testStream.delegate = self
testStream.open()
let xmlByteArray = [UInt8](profile.utf8)
testStream.write(xmlByteArray, maxLength: xmlByteArray.count)
testStream.close()
let outputData = testStream.property(forKey: Stream.PropertyKey.dataWrittenToMemoryStreamKey) as! Data
let outputBytes = [UInt8](outputData)
let outputXml = String(decoding: outputBytes, as: UTF8.self)
print(outputXml)
}
}
print("Launching stream test app")
let foo = TestClass()
foo.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment