Skip to content

Instantly share code, notes, and snippets.

@rahulsingh1101
Last active August 26, 2021 08:08
Show Gist options
  • Save rahulsingh1101/9c798337266d614aba7cb936bd28f912 to your computer and use it in GitHub Desktop.
Save rahulsingh1101/9c798337266d614aba7cb936bd28f912 to your computer and use it in GitHub Desktop.
extension Data {
func toString() -> String? {
let str = String(data: self, encoding: .utf8)
return str
}
func toDictionary() -> [String: Any]? {
do {
return try JSONSerialization.jsonObject(with: self, options: []) as? [String: Any]
} catch {
print("toDictionary conversion error : \(error.localizedDescription)")
}
return nil
}
var bytes: [UInt8] {
return [UInt8](self)
}
}
extension String {
func toData() -> Data? {
self.data(using: .utf8)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment