2016-09-20 189 views
0

我一直試圖讓這個工作兩個星期。它使用Swift 2在iOS 9上工作,但現在看起來沒有任何理由。我添加了print("[DEBUG] I was here 1/2/3/4")來調試代碼,打印出的所有代碼都是[DEBUG] I was here 1。有任何想法嗎?它讓我瘋狂。用Swift 3解析JSON

func downloadData() { 
    //clear the arrays 
    arrayPosts = [String]() 
    arrayLinks = [String]() 
    arrayConditions = [String]() 
    arrayIDs = [String]() 
    //debug 
    print("[DEBUG] I was here: 1") 

    //baseURL is a string with URL to JSON Endpoint 
    let url = URL(string: baseURL) 
    URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in 
     if error != nil { 
      print("[ERROR] [DEBUG] Error with connection: \(error)") 
     } else { 
      do { 
       //debug 
       print("[DEBUG] I was here: 2") 
       if let json = try JSONSerialization.jsonObject(with: data!, options:[.mutableContainers, .allowFragments]) as? [[String: Any]] { 
        for item in json { 
         //debug 
         print("[DEBUG] I was here: 3") 
         if let startTime = item["current_time"] as? Int { 
          if self.defaults.integer(forKey: "startTime") == 0 { 
           self.defaults.set(startTime, forKey: "startTime") 
          } 
         } 

         if let posts = item["posts"] as? [[String: AnyObject]] { 
          //debug 
          print("[DEBUG] I was here: 4") 
          for post in posts { 
           if let text = post["text"] as? String { 
            if let condition = post["conditions"] as? String{ 
             if let url = post["url"] as? String { 
              if let id = post["id"] as? String { 
               self.arrayPosts.append(text) 
               self.arrayConditions.append(condition) 
               self.arrayLinks.append(url) 
               self.arrayIDs.append(id) 
              } 
             } 
            } 
           } 
          } 

         } 
        } 
       } 
      } catch { 
       print("[ERROR] [DEBUG] Something went wrong during data download from the server.") 
      } 
     } 

    }).resume() 
} 

這裏的JSON數據,以防萬一:

{ 
    "status": "ok", 
    "num_results": "4", 
    "current_time": 1474386061, 
    "user_time": 0, 
    "posts": [ 
    { 
     "text": "If your shirt isn't tucked into your pants, then your pants are tucked into your shirt.", 
     "conditions": "4", 
     "url": "0", 
     "time": "0", 
     "id": "108" 
    }, 
    { 
     "text": "Veteran Kills Himself in Parking Lot of V.A. Hospital on Long Island", 
     "conditions": "6", 
     "url": "http://www.nytimes.com/2016/08/25/nyregion/veteran-kills-himself-in-parking-lot-of-va-hospital-on-long-island.html", 
     "time": 1472076000, 
     "id": "1472076000" 
    }, 
    { 
     "text": "Leaked Script Shows What Advisers Want Donald Trump to Say at Black Church", 
     "conditions": "6", 
     "url": "http://www.nytimes.com/2016/09/02/us/politics/donald-trump-black-voters-wayne-jackson.html", 
     "time": 1472767200, 
     "id": "1472767200" 
    }, 
    { 
     "text": "Creepy Clown Sightings in South Carolina Cause a Frenzy", 
     "conditions": "6", 
     "url": "http://www.nytimes.com/2016/08/31/us/creepy-clown-sightings-in-south-carolina-cause-a-frenzy.html", 
     "time": 1472594400, 
     "id": "1472594400" 
    } 
    ] 
} 
+0

我剛剛用假網址測試,它關係到「[DEBUG]我在這裏:2「。 – Moritz

+0

是'let url = URL(string:baseURL)'nil? – Larme

+0

@EricAya理論上它應該。我再次運行它,以防萬一它停在「[DEBUG]我在這裏:1」。可能是其他的錯誤?就像我使用http而不是https一樣? –

回答

-1

我已經找到了解決辦法。我將代碼移到了後臺隊列中。解決了這個問題。

0
func downloadData() { 
    //clear the arrays 
    arrayPosts = [String]() 
    arrayLinks = [String]() 
    arrayConditions = [String]() 
    arrayIDs = [String]() 
    //debug 
    print("[DEBUG] I was here: 1") 

    //baseURL is a string with URL to JSON Endpoint 
    let url = URL(string: baseURL) 
    let session = URLSession.shared 

    let task = session.dataTask(with: url!) { (data:Data?, response:URLResponse?, error:Error?) in 
     if error != nil { 
      print("[ERROR] [DEBUG] Error with connection: \(error)") 
     } else { 
      do { 
       //debug 
       print("[DEBUG] I was here: 2") 
       if let json = try JSONSerialization.jsonObject(with: data!, options:[.mutableContainers, .allowFragments]) as? [[String: Any]] { 
        for item in json { 
         //debug 
         print("[DEBUG] I was here: 3") 
         if let startTime = item["current_time"] as? Int { 
          if self.defaults.integer(forKey: "startTime") == 0 { 
           self.defaults.set(startTime, forKey: "startTime") 
          } 
         } 

         if let posts = item["posts"] as? [[String: AnyObject]] { 
          //debug 
          print("[DEBUG] I was here: 4") 
          for post in posts { 
           if let text = post["text"] as? String { 
            if let condition = post["conditions"] as? String{ 
             if let url = post["url"] as? String { 
              if let id = post["id"] as? String { 
               self.arrayPosts.append(text) 
               self.arrayConditions.append(condition) 
               self.arrayLinks.append(url) 
               self.arrayIDs.append(id) 
              } 
             } 
            } 
           } 
          } 

         } 
        } 
       } 
      } catch { 
       print("[ERROR] [DEBUG] Something went wrong during data download from the server.") 
      } 
     } 

    } 
    task.resume() 
} 
+0

這似乎並不奏效。它停在「[DEBUG]我又來了1」。 –

5

試試下面的代碼:

import UIKit 

class ViewController: UIViewController { 

    var arrayPosts = [String]() 
    var arrayLinks = [String]() 
    var arrayConditions = [String]() 
    var arrayIDs = [String]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.downloadData() 

    } 

    func downloadData() 
    { 
     let url = NSURL(string: "http://getmydetails.pe.hu/test/parsejsonswift3.php") //Provided JSON data on my server. i don't know how longer it is present there!. 
     let request = NSMutableURLRequest(url: url! as URL) 

     let task = URLSession.shared.dataTask(with: request as URLRequest) { data,response,error in 
     guard error == nil && data != nil else 
     { 
      print("Error:",error) 
      return 
      } 

      let httpStatus = response as? HTTPURLResponse 

      if httpStatus!.statusCode == 200 
      { 
       if data?.count != 0 
       { 
        let responseString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary //because JSON data started with dictionary. Not an array 
        let status = responseString["status"] as! String 

        if status == "ok" 
        { 
         print("Status is :", status) 

         let timevar = responseString["current_time"] //not specified as String or Int 
         print(String(describing: timevar)) // print by converting anyobject to string 

         if let posts = responseString["posts"] as? [AnyObject] // posts started with array 
         { 
         for post in posts 
          { 
           let text = post["text"] as! String //specify as String 
           print(text) 

           let condition = post["conditions"] as! String 
           let url = post["url"] as! String 
           let id = post["id"] as! String 
           DispatchQueue.main.sync 
           { 

            self.arrayPosts.append(text) 
            self.arrayConditions.append(condition) 
            self.arrayLinks.append(url) 
            self.arrayIDs.append(id) 

            //optional to check 
            self.afterJSON() 
           } 
          } 
         } 
         else 
         { 
          print("I could not find post array") 
         } 
        } 
        else 
        { 
         print("Status is not Okay!") 
        } 
       } 
       else 
       { 
        print("No data got from url!") 
       } 
      } 
      else 
      { 
       print("error httpstatus code is :",httpStatus!.statusCode) 
      } 
     } 
     task.resume() 
    } 

    func afterJSON() 
    { 
     print("Posts are:\n\t\t",arrayPosts) 
     print("Conditions are:\n\t\t",arrayConditions) 
     print("Links are:\n\t\t",arrayLinks) 
     print("ID's are:\n\t\t",arrayIDs) 

    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


} 

輸出就像下面的截圖,

enter image description here

如果你迷惑,然後看到我video是給你的。

如果有任何疑問,請發表評論如下:d

0

感謝的人我按照你的代碼中得到的數據))

func getJSON(){ 
    let wUrl = "http://api.fixer.io/latest" 
    let Url = NSURL(string: wUrl) 
    let request = NSMutableURLRequest(url: Url! as URL) 

    let task = URLSession.shared.dataTask(with: request as URLRequest){ data, response, error in 
     guard error == nil && data != nil else 
     { 
      print("Error:", error ?? "some error") 
      return 
     } 

     let httpStatus = response as? HTTPURLResponse 
     if httpStatus!.statusCode == 200 { 
      if data?.count != 0 { 

       let resposeString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary 

       let base = resposeString["base"] as? String 
       print(base ?? "") 
       let date = resposeString["date"] as? String 
       print(date ?? "") 
       let rates = resposeString["rates"] as! Dictionary<String, AnyObject> 

       for (key, value) in rates{ 
        print("\(key)->\(value)") 

       } 
      } 
      else 
      { 
       print("Data is emty") 
      } 
     } 
     else { 
      print("error httpStatus code: ", httpStatus?.statusCode ?? "") 
     } 

    };task.resume() 


}