Last active
August 26, 2021 08:08
-
-
Save rahulsingh1101/9c798337266d614aba7cb936bd28f912 to your computer and use it in GitHub Desktop.
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 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