SHARE
TWEET

Untitled

a guest Sep 16th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import UIKit
  2.  
  3. class TableViewCell: UITableViewCell {
  4.     let network = Network()
  5.     var bynString: String!
  6.    
  7.     @IBOutlet weak var bynTextField: UITextField!
  8.     @IBOutlet weak var outputLabel: UILabel!
  9.    
  10.     private let apikey = "71f3bc8d41181084e31b"
  11.     private let url = "https://free.currconv.com/api/v7/convert"
  12.     private var myCurrency: Response?
  13.    
  14.     func getCurrentCurrency(complition: @escaping (Response)->Void) {
  15.         var urlComponents = URLComponents(string: url)
  16.         urlComponents?.queryItems = [
  17.             URLQueryItem(name: "q", value: "USD_BYN,BYN_USD"),
  18.             URLQueryItem(name: "compact", value: "ultra"),
  19.             URLQueryItem(name: "apiKey", value: "\(apikey)")
  20.         ]
  21.         let url = urlComponents?.url
  22.        
  23.         let task = URLSession.shared.dataTask(with: url!) {
  24.             (data, response, error) in
  25.            
  26.             guard let data = data else { return }
  27.             guard let currency: Response = try? JSONDecoder().decode(Response.self, from: data) else {
  28.                 print("Error can't parse")
  29.                 return
  30.             }
  31.             return complition(currency)
  32.         }
  33.         task.resume()
  34.        
  35.     }
  36.  
  37.    
  38.  
  39.     override func awakeFromNib() {
  40.         super.awakeFromNib()
  41.         bynTextField.delegate = self
  42.         getCurrentCurrency {
  43.             (myCurrency) in
  44.         }
  45.    
  46.     }
  47.  
  48.     override func setSelected(_ selected: Bool, animated: Bool) {
  49.         super.setSelected(selected, animated: animated)
  50.  
  51.         // Configure the view for the selected state
  52.     }
  53.    
  54. }
  55.  
  56.     extension TableViewCell: UITextFieldDelegate {
  57.         func textFieldDidBeginEditing(_ textField: UITextField) {
  58.             if textField == bynTextField {
  59.                 if myCurrency?.usd_byn != nil {
  60.                 if textField.text != nil {
  61.                     let doubleText = Double(textField.text!)
  62.                     let result = doubleText! / (myCurrency?.byn_usd!)!
  63.                     outputLabel.text = String(result)
  64.                 }
  65.             }
  66.         }
  67.     }
  68.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top