I am very new to Swift coming from Obj C background. I am struggling to access a couple of key value pairs in a dictionary that comes from some JSON returned from an API endpoint. I can get as far as "data" but not sure how to get access to the arrays below that key. Specifically I am trying to return values for "id" and "price" as follows:
id = 1;
price = "20160.67609688202"
I would appreciate any help you might offer. Thank you.
Swift:
@IBAction func buttonAction(_ sender: Any) {
// Create the URL
let query = txt_Symbol.text
let api = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?CMC_PRO_API_KEY=myKey&symbol="
let endpoint = query!
let url = URL(string: api + endpoint)
guard let requestUrl = url else { fatalError() }
// Create URL Request
var request = URLRequest(url: requestUrl)
// Specify HTTP Method to use
request.httpMethod = "GET"
// Send HTTP Request
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
// Check if Error took place
if let error = error {
print("Error took place \(error)")
return
}
// Read HTTP Response Status code
if let response = response as? HTTPURLResponse {
print("Response HTTP Status code: \(response.statusCode)")
}
// Convert HTTP Response Data to a simple String
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print("Response data string:\n \(dataString)")
do {
if let convertedJsonIntoDict = try JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
// Print out entire dictionary
print("convertedJsonIntoDict:")
print(convertedJsonIntoDict)
// UI API called on a background thread
// Call this on the main thread
DispatchQueue.main.async {
//Do UI Code here.
self.txtarea_JSON.text = convertedJsonIntoDict.description
// Get a value by key
let coinData = convertedJsonIntoDict["data"]
print(coinData ?? "coin could not be read")
// how do I access these key value pairs?
// id = 1;
// price = "20160.67609688202"
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
task.resume()
}
JSON:
{
data = {
BTC = {
"circulating_supply" = 19138912;
"cmc_rank" = 1;
"date_added" = "2013-04-28T00:00:00.000Z";
id = 1;
"is_active" = 1;
"is_fiat" = 0;
"last_updated" = "2022-09-02T03:39:00.000Z";
"max_supply" = 21000000;
name = Bitcoin;
"num_market_pairs" = 9718;
platform = "<null>";
quote = {
USD = {
"fully_diluted_market_cap" = "423374198034.52";
"last_updated" = "2022-09-02T03:39:00.000Z";
"market_cap" = "385853405678.7285";
"market_cap_dominance" = "39.0616";
"percent_change_1h" = "0.83296442";
"percent_change_24h" = "0.16774456";
"percent_change_30d" = "-11.73989491";
"percent_change_60d" = "5.58985077";
"percent_change_7d" = "-6.59123025";
"percent_change_90d" = "-31.87091684";
price = "20160.67609688202";
tvl = "<null>";
"volume_24h" = "29753736862.94145";
"volume_change_24h" = "-7.2446";
};
};
"self_reported_circulating_supply" = "<null>";
"self_reported_market_cap" = "<null>";
slug = bitcoin;
symbol = BTC;
tags = (
mineable,
pow,
"sha-256",
"store-of-value",
"state-channel",
"coinbase-ventures-portfolio",
"three-arrows-capital-portfolio",
"polychain-capital-portfolio",
"binance-labs-portfolio",
"blockchain-capital-portfolio",
"boostvc-portfolio",
"cms-holdings-portfolio",
"dcg-portfolio",
"dragonfly-capital-portfolio",
"electric-capital-portfolio",
"fabric-ventures-portfolio",
"framework-ventures-portfolio",
"galaxy-digital-portfolio",
"huobi-capital-portfolio",
"alameda-research-portfolio",
"a16z-portfolio",
"1confirmation-portfolio",
"winklevoss-capital-portfolio",
"usv-portfolio",
"placeholder-ventures-portfolio",
"pantera-capital-portfolio",
"multicoin-capital-portfolio",
"paradigm-portfolio"
);
"total_supply" = 19138912;
"tvl_ratio" = "<null>";
};
};
status = {
"credit_count" = 1;
elapsed = 35;
"error_code" = 0;
"error_message" = "<null>";
notice = "<null>";
timestamp = "2022-09-02T03:41:38.218Z";
};
}