2016-03-05 95 views
0

我正在創建一個簡單的應用程序,使用SWAPI顯示星球大戰中每個角色的詳細信息。現在我試圖獲得物種,但Xcode告訴我,當試圖打印_species時發現一個零,同時解開可選值。下面的代碼:解析JSON它不適用於Swift

func DownlaodCompleted(complete: DownloadComplete) { 
    let url = NSURL(string: _urlperson)! 
    Alamofire.request(.GET, url).responseJSON { (response: Response<AnyObject, NSError>) -> Void in 
     let result = response.result 

     if let dict = result.value as? Dictionary<String, AnyObject> { 

      if let height = dict["height"] as? String { 
       self._height = height 

      } 

      if let gender = dict["gender"] as? String { 
       self._gender = gender 
      } 

      if let birthYear = dict["birth_year"] as? String { 
       self._birthYear = birthYear 
      } 

      if let species = dict["species"] as? [Dictionary<String, String>] { 
       let urlSpecies = NSURL(string: self._urlSpecies)! 
       Alamofire.request(.GET, urlSpecies).responseJSON(completionHandler: { (response2: Response<AnyObject, NSError>) -> Void in 
        let result = response2.result 

        if let speciesDict = result.value as? Dictionary<String, AnyObject> { 
         if let name = speciesDict["name"] as? String { 
          self._species = name 
         } 
        } 
       }) 

      } 
      print(self._species) 
      print(self._height) 
     } 
    } 
} 

,這裏的類常量:

let URL_BASE = "http://swapi.co" 
let URL_PEOPLE = "/api/people/" 
let URL_SPECIES = "/api/species/1/" 

typealias DownloadComplete =() -> () 
+0

檢查每個強制解包操作(例如您的URL構造)。 –

+0

我認爲沒關係,我沒有看到有任何問題 –

+1

:-)如果他們沒事,我們就不會有這個討論! – Russell

回答

0

下面是我創建一個空的「單查看項目」,並加入2「豆莢」 Alamofire後使用示例代碼,和SwiftyJSON到項目。

示例代碼包含您的某個網址和註釋掉另一個網站的網址。

import UIKit 
import Alamofire 
import SwiftyJSON // No such module "SwiftyJSON" 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 
    @IBOutlet var tableView: UITableView! 
    var arrResults = [[String:AnyObject]]() //Array of dictionary 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480) 
     self.tableView = UITableView(frame:self.view!.frame) 
     self.tableView!.delegate = self 
     self.tableView!.dataSource = self 
     self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "jsonCell") 
     self.view?.addSubview(self.tableView) 

     Alamofire.request(.GET, "http://swapi.co/api/people/?format=json").responseJSON { (responseData) -> Void in 
      let swiftyJsonVar = JSON(responseData.result.value!) 
      for element in swiftyJsonVar["results"].arrayValue { 
       let name = element["name"].string! 
       let homeworld = element["homeworld"].string! 
       let object : Dictionary<String, String> = ["name": name, "homeworld": homeworld] 
       self.arrResults.append(object) 
      } 
      if self.arrResults.count > 0 { 
       self.tableView.reloadData() 
      } 
     } 

//  Alamofire.request(.GET, "http://api.androidhive.info/contacts/").responseJSON { (responseData) -> Void in 
//   let swiftyJsonVar = JSON(responseData.result.value!) 
//    
//   if let resData = swiftyJsonVar["contacts"].arrayObject { 
//    self.arrResults = resData as! [[String:AnyObject]] 
//   } 
//   if self.arrResults.count > 0 { 
//    self.tableView.reloadData() 
//   } 
//  } 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "jsonCell") 

     var dict = arrResults[indexPath.row] 
// Use these lines with: "http://swapi.co/api/people/?format=json" 
     cell.textLabel?.text = dict["name"] as? String 
     cell.detailTextLabel?.text = dict["homeworld"] as? String 
// Use these lines with: "http://api.androidhive.info/contacts/" 
//  cell.textLabel?.text = dict["name"] as? String 
//  cell.detailTextLabel?.text = dict["email"] as? String 
     return cell 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return arrResults.count 
    } 
}