0

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

4
  • I’m a little confused. Your model looks nothing like the data in the first code block. Commented Aug 12, 2021 at 20:13
  • woops, I think I pasted the wrong model, give me a sec @jnpdx Commented Aug 12, 2021 at 20:13
  • @jnpdx corrected, good catch! Commented Aug 12, 2021 at 20:14
  • @jnpdx I added a couple of details on the actual data that is printed that might be useful Commented Aug 12, 2021 at 20:25

1 Answer 1

2

Your custom type conforms to the Codable protocol so make use of that instead

func convertChainsIntoJSON(chains: [AllChainsItemsClassAModel]) -> String? {
    guard let data = try? JSONEncoder().encode(chains) else { return nil }
    return String(data: data, encoding: String.Encoding.utf8)
}
Sign up to request clarification or add additional context in comments.

1 Comment

that worked beautifully and less code! Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.