I’d like to create a nested JSON string of installed font families and their associated fonts (which I could later send to a webview) using Swift 5.
I'm having trouble understanding Swift nested objects. Is there a specific type of object structure required by JSONSerialization?
var obj: [String: Any] = [:]
for family in UIFont.familyNames {
var fonts: [String] = []
for font in UIFont.fontNames(forFamilyName: family) {
fonts.append(font)
}
obj[family] = fonts
}
do {
let json = try JSONSerialization.data(withJSONObject: obj, options: .prettyPrinted)
print(json) // Outputs ‘9415 bytes’ rather than JSON string (below)
} catch {}
I would like the JSON string to look something like:
{
“Didot”:[“Didot”,”Didot-Italic”,”Didot-Bold”],
“Impact”:[”Impact”],
// etc
}