-
Notifications
You must be signed in to change notification settings - Fork 2
/
ViewController.swift
executable file
·101 lines (80 loc) · 2.97 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// ViewController.swift
// AwesomeConverter
//
// Created by Student on 2/25/19.
// Copyright © 2019 Student. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var myCurrency:[String] = []
var myValues:[Double] = []
var activeCurrency:Double = 0;
//OBJECTS
@IBOutlet weak var Input: UITextField!
@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var output: UILabel!
//CREATING PICKER VIEW
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return myCurrency.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return myCurrency[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
activeCurrency = myValues[row]
}
//BUTTON
@IBAction func action(_ sender: AnyObject) {
if (Input.text != "")
{
output.text = String(Double(Input.text!)! * activeCurrency)
}
}
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//GETTING DATA
let url = URL(string: "http://www.apilayer.net/api/live?access_key=1b759db15c251757651faf643ca2815b&fbclid=IwAR29w_d3AzVLPsNkvhx6CbZjuy_RKx00eyLOUcsB2x5MrLVYAI11ipDWF6M")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil
{
print ("ERROR")
}
else
{
if let content = data
{
do
{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let quotes = myJson["quotes"] as? NSDictionary
{
for (key, value) in quotes
{
self.myCurrency.append((key as? String)!)
self.myValues.append((value as? Double)!)
}
//print (self.myCurrency)
//print (self.myValues)
}
}
catch
{
}
}
}
self.pickerView.reloadAllComponents()
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}