Advertisement
Guest User

Untitled

a guest
Feb 18th, 2016
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.92 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  json
  4. //
  5. //  Created by admin on 18.02.16.
  6. //  Copyright © 2016 admin. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.    
  13.    
  14.     //GLOBAL VARIABLES
  15.    
  16.     var globalJsonLink = "http://www.learnswiftonline.com/Samples/subway.json"
  17.    
  18.     //OUTLETS
  19.     @IBOutlet weak var statusLabel: UILabel!
  20.     @IBOutlet weak var localhostSwitch: UISwitch!
  21.    
  22.    
  23.     //ACTIONS
  24.     @IBAction func jsonButton(sender: UIButton) {
  25.    
  26.         callSubwayJson()
  27.         //     callRandomPersonJson()
  28.     }
  29.    
  30.    
  31.     @IBAction func localhostSwitched(sender: AnyObject) {
  32.        
  33.         if localhostSwitch.on
  34.         {
  35.             globalJsonLink = "http://localhost/bonus/api/long.json"
  36.         }
  37.         else
  38.         {
  39.               globalJsonLink = "http://www.learnswiftonline.com/Samples/subway.json"
  40.            
  41.         }
  42.        
  43.     }
  44.    
  45.    
  46.    
  47.     override func viewDidLoad() {
  48.         super.viewDidLoad()
  49.         // Do any additional setup after loading the view, typically from a nib.
  50.     }
  51.  
  52.     override func didReceiveMemoryWarning() {
  53.         super.didReceiveMemoryWarning()
  54.         // Dispose of any resources that can be recreated.
  55.     }
  56.  
  57.  
  58. // SUBWAY JSON
  59.     func callSubwayJson(){
  60.    
  61.         //CHANGE requestURL value depending on the localhostSwitch.on state
  62.        
  63.  
  64.    
  65.         let requestURL: NSURL = (NSURL(string: globalJsonLink))!
  66.         let urlRequest: NSMutableURLRequest = NSMutableURLRequest(URL: requestURL)
  67.         let session = NSURLSession.sharedSession()
  68.         let task = session.dataTaskWithRequest(urlRequest) {
  69.             (data, response, error) -> Void in
  70.            
  71.             let httpResponse = response as! NSHTTPURLResponse
  72.             let statusCode = httpResponse.statusCode
  73.            
  74.             if (statusCode == 200) {
  75.                
  76.                 do{
  77.                    
  78.                     let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)
  79.                    
  80.                     if let stations = json["stations"] as? [[String: AnyObject]] {
  81.                        
  82.                         for station in stations {
  83.                            
  84.                             if let name = station["stationName"] as? String {
  85.                                
  86.                                 if let year = station["buildYear"] as? String {
  87.                                     NSLog("%@ (Built %@)",name,year)
  88.                                 }
  89.                                
  90.                             }
  91.                         }
  92.                        
  93.                     }
  94.                    
  95.                 }catch {
  96.                     print("Error with Json: \(error)")
  97.                    
  98.                 }
  99.                
  100.             }
  101.            
  102.         }
  103.        
  104.         task.resume()
  105.    
  106.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement