Facebook
From Diminutive Bushbaby, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 244
  1.  func start(){
  2.         let url = URL(string: "https://isebi.net/albums.php")
  3.         
  4.        
  5.             let task = URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
  6.                 if let error = error {
  7.                     print(error)
  8.                 }
  9.                 
  10.                 if let data = data{
  11.                     print("data =\(data)")
  12.                 }
  13.                 if let response = response {
  14.                     let httpResponse = response as! HTTPURLResponse
  15.                     
  16.                     if httpResponse.statusCode == 200{
  17.                         do {
  18.                             let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSArray //
  19.                             
  20.                             for jsonObj2 in json{
  21.                                 //if((jsonObj as? NSNull) == nil){
  22.                                 if( jsonObj2 is NSNull) {
  23.                                     continue;
  24.                                 }
  25.                                 let jsonObj = jsonObj2 as! NSDictionary;
  26.                                 let cd = CD();
  27.                                 if ( jsonObj["artist"] is NSNull ) {
  28.                                    continue;
  29.                                 }
  30.                                 if ( jsonObj["tracks"] is NSNull ) {
  31.                                     continue;
  32.                                 }
  33.                                 if ( jsonObj["album"] is NSNull ) {
  34.                                     continue;
  35.                                 }
  36.                                 if ( jsonObj["genre"] is NSNull ) {
  37.                                     continue;
  38.                                 }
  39.                                 if ( jsonObj["year"] is NSNull ) {
  40.                                     continue;
  41.                                 }
  42.                                 
  43.                                 
  44.                                 let artist = jsonObj["artist"] as! String;
  45.                                 let tracks = jsonObj["tracks"] as! Int;
  46.                                 let album = jsonObj["album"] as! String;
  47.                                 let year = jsonObj["year"] as! Int;
  48.                                 let genre = jsonObj["genre"] as! String;
  49.                                 cd.Artist = artist;
  50.                                 cd.Genre = genre;
  51.                                 cd.Title = album;
  52.                                 cd.Year = year;
  53.                                 cd.Tracks = tracks;
  54.                                 self.CDArray.append(cd);
  55.                                 print(jsonObj);
  56.                                 //}
  57.                             }
  58.                             
  59.                             DispatchQueue.main.async {
  60.                                 self.buttons();
  61.                                 self.updateView();
  62.                             }
  63.                         } catch let error as NSError {
  64.                             print(error)
  65.                         }
  66.                         
  67.                     } else {
  68.                         print("NO");
  69.                     }
  70.                 }
  71.             })
  72.             task.resume()
  73.         
  74.     }