Created
December 26, 2018 10:02
-
-
Save ysoftware/f32cc57e96c849990a0df6cac16b66cf to your computer and use it in GitHub Desktop.
Convert Bool to Data and back
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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