Created
July 29, 2016 18:12
-
-
Save trylovetom/b620bd7f04082a4a96994c2c086d3e75 to your computer and use it in GitHub Desktop.
for Birkhoff Lee
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
// | |
// ViewController.swift | |
// test | |
// | |
// Created by 張子晏 on 2016/7/30. | |
// Copyright © 2016年 張子晏. All rights reserved. | |
// | |
import UIKit | |
import SwiftHTTP | |
class ViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
convertCurrency({ double, error in | |
guard error == nil else { | |
print(error) | |
return | |
} | |
if let number = double { | |
print(number) | |
} | |
}) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func convertCurrency(callback: (Double?, String?) -> Void) { | |
do { | |
let opt = try HTTP.GET("http://apilayer.net/api/live?access_key=8f38982e14966d223058041d9307f536¤cies=TWD&source=USD&format=1") | |
opt.start { response in | |
if let err = response.error { | |
callback(0.0, err.localizedDescription); | |
} | |
do { | |
let json = try NSJSONSerialization.JSONObjectWithData(response.data, options: NSJSONReadingOptions.MutableContainers) | |
if let quotes = json["quotes"] { | |
if let q = quotes { | |
if let usdtwd = q["USDTWD"] { | |
callback(usdtwd as? Double, nil) | |
} | |
} | |
} | |
} catch { | |
callback(0.0, "nsjosn error") | |
} | |
} | |
} catch { | |
callback(0.0, "http get error") | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment