Skip to content

Instantly share code, notes, and snippets.

@ysoftware
Created December 26, 2018 10:02
Show Gist options
  • Save ysoftware/f32cc57e96c849990a0df6cac16b66cf to your computer and use it in GitHub Desktop.
Save ysoftware/f32cc57e96c849990a0df6cac16b66cf to your computer and use it in GitHub Desktop.
Convert Bool to Data and back
extension Bool {
var data:NSData {
var _self = self
return NSData(bytes: &_self, length: MemoryLayout.size(ofValue: self))
}
init?(data:NSData) {
guard data.length == 1 else { return nil }
var value = false
data.getBytes(&value, length: MemoryLayout<Bool>.size)
self = value
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment