This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fetch() { | |
// requestSampleJSON() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { | |
return "Cryptocurrency Prices" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let tableViewCell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath) | |
if let cryptoTableViewCell = tableViewCell as? CryptoTableViewCell { | |
let currencyType = currencies[indexPath.row] | |
cryptoTableViewCell.formatCell(withCurrencyType: currencyType) | |
} | |
return tableViewCell | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return currencies.count | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let currencies: [CurrencyType] = [.btc, .eth, .ltc, .xrp, .xmr, .neo] | |
let reuseIdentifier = String(describing: CryptoTableViewCell.self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func formatCell(withCurrencyType currencyType: CurrencyType) { | |
currencyName.text = currencyType.name | |
currencyImageView.image = currencyType.image | |
currencyType.requestValue { (value) in | |
DispatchQueue.main.async { | |
self.currencyPrice.text = value?.formattedCurrencyString ?? "Failed to get price" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private extension NSNumber { | |
var formattedCurrencyString: String? { | |
let formatter = NumberFormatter() | |
formatter.locale = Locale(identifier: "en_US") | |
formatter.numberStyle = .currency | |
return formatter.string(from: self) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let request = URLSession.shared.dataTask(with: apiURL) { (data, response, error) in | |
// TODO: Get the data and return it | |
} | |
request.resume() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func requestValue(completion: @escaping (_ value: NSNumber?) -> Void) { | |
guard let apiURL = apiURL else { | |
// If we couldn't serialize the JSON set the value in the completion as nil and print the error | |
completion(nil) | |
print("URL Invalid") | |
return | |
} | |
} |
NewerOlder