gpt4 book ai didi

ios - 解析 JSON 它在 Swift 中不起作用

转载 作者:行者123 更新时间:2023-11-30 13:38:43 25 4
gpt4 key购买 nike

我正在创建一个简单的应用程序,使用 SWAPI 显示《星球大战》中每个角色的详细信息。现在我正在尝试获取物种,但 Xcode 告诉我在尝试打印 _species 时解开可选值时发现了 nil。代码如下:

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 = () -> ()

最佳答案

这是我在创建空的“单 View 项目”并将 2 个“pod”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
}
}

关于ios - 解析 JSON 它在 Swift 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35817132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com