launchdata.json:
[
{
"name": "Smartcard",
"imageName": "Smartcard",
"description": "A all new way to buy and sell stuff featuring RFID and NFC for touchless transactions and with local server you can pay with your phone.",
"department": "Ticki Finance",
"productid": 1737484,
"id": 1,
"creator": "The Ticki Team"
},
{
"name": "Ink pad",
"imageName": "Inkpad",
"description": "A quick and easy way to take fingerprints and stamp stamps:), And with a quick water activation taking only 15 seconds you can setup in no time. Also, refilling the ink chamber is super easy, all you have to do is put ink in the middle hole.",
"department": "Ticki Design",
"productid": 7338388,
"id": 2,
"creator": "The Ticki Team"
},
{
"name": "Wallet",
"imageName": "Wallet",
"description": "Ever had issues with your credit cards falling out of your pocket/wallet? Well this fixes any issues. Introducing Ticki Wallet. ",
"department": "Ticki Finance",
"productid": 2444495,
"id": 3,
"creator": "The Ticki Team"
},
{
"name": "Pencil Case",
"imageName": "PencilCase",
"description": "I always lose my my pencils. How about you? Well i'm fixing that today with ticki pencil case. A pencil case that can hold pencils, an eraser, and of course, tickies.",
"department": "Ticki Design",
"productid": 3840398,
"id": 4,
"creator": "The Ticki Team"
}
]
Load function:
func load<T: Decodable>(_ filename: String) -> T {
guard let fileUrl = Bundle.main.url(forResource: filename, withExtension: nil) else {
fatalError("Couldn't find \(filename) in main bundle.")
}
do {
let data = try Data(contentsOf: fileUrl)
let decoder = JSONDecoder()
let decodedObject = try decoder.decode(T.self, from: data)
return decodedObject
} catch {
fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
}
}
Error:
Thread 1: Fatal error: Couldn't parse Launchdata.json as Array<Product>:
dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around line 1, column 0." UserInfo={NSDebugDescription=Invalid value around line 1, column 0., NSJSONSerializationErrorIndex=0})))