简体   繁体   English

Alamofire JSON请求失败

[英]Alamofire json requests failed

So I have the following code 所以我有以下代码

@IBOutlet var jokes_list: UITableView!
var CountCells = 0
var CellsData = [NSArray: NSArray]()

override func viewDidLoad() {

    super.viewDidLoad();

    Alamofire.request("http://localhost:8080/jokes.php").responseJSON{ response in
        debugPrint(response.result.value)   // result of response serialization

        if let JSON = response.result.value as? [NSArray: NSArray] {
            self.CellsData = JSON
            self.CountCells = JSON.count
            self.jokes_list.reloadData()
        }else{
            debugPrint("failed")
        }
    }



    // Do any additional setup after loading the view, typically from a nib.
}

I get the debugPrint(response.result.value) output, but I also get a failed from the else statement. 我得到debugPrint(response.result.value)输出,但是我从else语句也failed了。

A bit of the output from the debugPrint(response.result.value) debugPrint(response.result.value)的输出结果

Optional(<__NSArrayI 0x7fc9c6056a00>(
{
    added = "2017-01-10 19:07:14";
    description = "Ora de religie \U00een scoala. Profesoara: \U2013 Si copii, amintiti-va permanent: cei care vor \U00eenvata pentru note de 9 si 10 vor nimeri \U00een

It's a very long output, so I don't think I can paste everything here, however the question is why can't I set the json as an array to CellsData in the code, so I can use it to show the list. 这是一个很长的输出,所以我想我不能在这里粘贴所有内容,但是问题是为什么我CellsData在代码中将json设置为CellsData的数组,所以可以使用它来显示列表。

as? [NSArray: NSArray] as? [NSArray: NSArray] definitively isn't correct, there-for the cast fails and the else branch is executed. as? [NSArray: NSArray]绝对不正确,因此-强制转换失败,并执行else分支。 It means that you are expecting a dictionary where keys and values are of type NSArray , but you have an array or dictionaries, so it should read as? [[String: Any]] 这意味着您期望一个字典,其中键和值的类型为NSArray ,但是您有一个数组或字典,因此应将其读as? [[String: Any]] as? [[String: Any]] or similar, depending on the response you didn't provide fully. as? [[String: Any]]或类似内容,取决于您未完全提供的响应。

Also var CellsData = [NSArray: NSArray]() must pe changed respectively. 另外,必须分别更改var CellsData = [NSArray: NSArray]() (btw: please familiarize yourself with cocoa touch / swift naming conventions) (顺便说一句:请熟悉可可触摸/快速命名约定)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM