I have a function created to convert an Array to JSON, I wanted to use it for my Model since I have to convert it to an AWSJSON and it's not working.
Here's the error I get:
Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)
in line guard let data = try? JSONSerialization... below in the function
Here's what the array contains
let chainsWithBalance = await tokenBalancesClassAViewModel.getChainsOnlyWithWalletBalance(wallet: walletAddress)
Now... when I print the variable above I get:
[appName.AllChainsItemsClassAModel(name: Optional("eth-mainnet"), chain_id: Optional("1"), db_schema_name: Optional("chain_eth_mainnet"), label: Optional("Ethereum Mainnet"), logo_url: Optional("https://www.covalenthq.com/static/images/icons/display-icons/ethereum-eth-logo.png"), is_testnet: Optional(false)), appName.AllChainsItemsClassAModel(name: Optional("matic-mainnet"), chain_id: Optional("137"), db_schema_name: Optional("chain_matic_mainnet"), label: Optional("Matic Mainnet"), logo_url: Optional("https://www.covalenthq.com/static/images/icons/display-icons/polygon-matic-logo.png"), is_testnet: Optional(false)), appName.AllChainsItemsClassAModel(name: Optional("bsc-mainnet"), chain_id: Optional("56"), db_schema_name: Optional("chain_bsc"), label: Optional("Binance Smart Chain"), logo_url: Optional("https://www.covalenthq.com/static/images/icons/display-icons/binance-coin-bnb-logo.png"), is_testnet: Optional(false))
]
Could it be that I have to loop through it and then re-assign to a new set of arrays unwrapping it?
Here's the function I'm using to convert it to JSON
func convertChainsIntoJSON(chains: [AllChainsItemsClassAModel]) -> String? {
guard let data = try? JSONSerialization.data(withJSONObject: chains, options: []) else {
return nil
}
return String(data: data, encoding: String.Encoding.utf8)
}
Here's the model I'm using:
struct AllChainsItemsClassAModel: Codable, Hashable {
let name, chain_id, db_schema_name, label, logo_url: String?
let is_testnet: Bool?
}
I'm puzzled by it, what's going on? The function works fine to convert an array of type ["a", "b"]
Thanks