Hi Guys,
I have a java api that returns me a json with the pdf document as byte array. I need to convert byte array to pdf in swift 3.
Thanks in advance
Hi Guys,
I have a java api that returns me a json with the pdf document as byte array. I need to convert byte array to pdf in swift 3.
Thanks in advance
There’s three parts to this:
Parsing JSON in general (A)
Converting a JSON byte array to a Swift
Data value (B)Using that as a PDF (C)
Reading through your question I wasn’t sure which bit you’re stuck on but I suspect it’s B. If so, here’s a small snippet that shows how you can do this:
import Foundation
let json = "{ \"bytes\": [12, 34, 56, 78] }"
let jsonData = json.data(using: .utf8)!
let jsonRoot = (try! JSONSerialization.jsonObject(with: jsonData, options: [])) as! [String: Any]
let bytes = jsonRoot["bytes"] as! [UInt8]
let data = Data(bytes)
print(data as NSData)Lines 1 through 4 are there solely to set up some test data. Lines 5 and 6 parse the JSON to get the byte array. Line 7 is key: it constructs a
Data value from the array of
UInt8. Line 8 prints the result, namely:
<0c22384e>Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"