使用 Alamofire 时出错

Posted

技术标签:

【中文标题】使用 Alamofire 时出错【英文标题】:Error when using Alamofire 【发布时间】:2016-03-27 17:43:09 【问题描述】:

我现在使用 Alamofire 和 SwiftyJSON。我按照这个教程http://ashishkakkad.com/2015/10/how-to-use-alamofire-and-swiftyjson-with-swift-swift-2-ios-9-xcode-7/

我的代码:

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UIViewController, UITableViewDataSource 

    @IBOutlet var tblJSON: UITableView!
    var arrRes = [[String:AnyObject]]()

    override func viewDidLoad() 
        super.viewDidLoad()
        tblJSON.dataSource = self

//        Alamofire.request(.GET, "http://api.androidhive.info/contacts/").response  (request, response, data, error) -> Void in
//            print(response)
//            let outputString = NSString(data: data!, encoding: NSUTF8StringEncoding)
//            print(outputString)
//        

        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.arrRes = resData as! [[String:AnyObject]]
            
            if self.arrRes.count > 0 
                self.tblJSON.reloadData()
            
        
    

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        return arrRes.count
    

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
        let cell = tblJSON.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        var dict = arrRes[indexPath.row]
        cell.textLabel?.text = dict["name"] as? String
        cell.detailTextLabel?.text = dict["email"] as? String
        return cell
       

但它在这一行崩溃了:

let swiftyJsonVar = JSON(responseData.result.value!)

我的 Pod 文件:

# Uncomment this line to define a global platform for your project
platform :ios, '8.0'
# Uncomment this line if you're using Swift
use_frameworks!

target 'UsingAlamofireAndSwiftyJSON' do
    pod 'Alamofire', '~> 3.2.1'
    pod 'SwiftyJSON', :git => 'https://github.com/SwiftyJSON/SwiftyJSON.git'
end

我使用 Xcode 7.2、Swift 2.1。任何帮助将不胜感激,谢谢。

【问题讨论】:

通过使用 responseData.result.value! 强制解开 Alamofire 的 Optional 结果,您是在告诉编译器:“如果没有价值,请让我的应用崩溃。”它被告知... // 解决方案是阅读this chapter of the free online Swift documentation: "Optionals". 【参考方案1】:

Alamofire 和 SwiftyJSON 非常了解它,以避免强制解包选项,您可以将响应处理程序更改为 JSON 响应处理程序,如下所示:

Alamofire.request(.GET, "http://api.androidhive.info/contacts/").responseJSON  response in

        switch(response.result) 
        case .Success(let value):      
            let json = JSON(value)  
        case .Failure(let error):
            print(error.description)  
        
    

使用上面的代码,您不需要解包可选,正如@Eric 在他的评论中所说,不推荐强制解包。

希望对你有所帮助。

【讨论】:

感谢您的建议。

以上是关于使用 Alamofire 时出错的主要内容,如果未能解决你的问题,请参考以下文章

使用 Alamofire 快速更新个人资料时出错?

使用 Alamofire 解码 json 时出错

在类函数中调用 Alamofire 时出错

使用消息“Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType”从存储中获取图像时出错

使用 Alamofire 搜索从 swift 2 转换为 swift 3 时出错

使用 Alamofire(多部分数据)上传带有额外参数的 .PDF 文件时出错