Data - leacode/SwiftWings GitHub Wiki
Source: Sources/Extensions/Foundation/Data
Tests: Tests/Extensions/Data
- Adds a
bytesproperty that returns[UInt8], simplifying round-trips between Foundation and low-level algorithms.
-
prettyJSONStringprints JSON payloads with indentation usingJSONSerialization. - Convenience initializer
Data(json:)along withjsonToDictionary()/jsonToArray()helpers make it trivial to hydrate Foundation collections.
- Inspects magic headers to classify dozens of audio/image/application file types (
isPng,isMp3,isTar, etc.). - Backed by
mimeTypesdefinitions plus enums (AudioMimeType,FileType) and used heavily inData+MimeTypeTestsfixtures.
let jsonData = #"{"name":"SwiftWings"}"#.data(using: .utf8)!
print(jsonData.prettyJSONString ?? "n/a")
// {
// "name" : "SwiftWings"
// }
if let pngURL = Bundle.module.url(forResource: "sample", withExtension: "png"),
let pngData = try? Data(contentsOf: pngURL) {
print(pngData.isPng) // true
print(pngData.bytes.prefix(4)) // [137, 80, 78, 71]
}