onsdag den 14. juni 2017

Swift OutputStream Quirk

Writing a zero length array to an OutputStream will effectively close it. The following example demonstrates the issue.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
let os = OutputStream(toFileAtPath: path, append: false)!
os.open()
print(os.hasSpaceAvailable)
let d = Data(count: 0)
let written = d.withUnsafeBytes { bytes in
    return os.write(bytes, maxLength: d.count)
}
print(os.hasSpaceAvailable)
os.close()
print(os.hasSpaceAvailable)