Data - leacode/SwiftWings GitHub Wiki

Data Extensions & MIME Utilities

Source: Sources/Extensions/Foundation/Data

Tests: Tests/Extensions/Data

Data+Extensions.swift

  • Adds a bytes property that returns [UInt8], simplifying round-trips between Foundation and low-level algorithms.

Data+PrettyJSON.swift

  • prettyJSONString prints JSON payloads with indentation using JSONSerialization.
  • Convenience initializer Data(json:) along with jsonToDictionary() / jsonToArray() helpers make it trivial to hydrate Foundation collections.

Data+MimeType.swift & MimeType.swift

  • Inspects magic headers to classify dozens of audio/image/application file types (isPng, isMp3, isTar, etc.).
  • Backed by mimeTypes definitions plus enums (AudioMimeType, FileType) and used heavily in Data+MimeTypeTests fixtures.

Example

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]
}
⚠️ **GitHub.com Fallback** ⚠️