簡體   English   中英

使用Alamofire和Bearer令牌序列化對象

[英]Serializing Objects with Alamofire and bearer token

在IOS 8,Xcode 6.3和swift項目中,使用出色的Alamofire庫v1.2,我正在嘗試從JSON-API響應序列化對象,我想知道哪種是實現此目標的最佳方法。 我認為以下代碼中的主要問題是:

  • 在類控制器中,println(data)顯示nil。
  • 在俱樂部對象類中,位置屬性未正確映射。

JSON-API響應為:

hits = [{
  "_id" : "5470def9e0c0be27780121d7",
  "imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/5470def9e0c0be27780121d7_180.png",
  "name" : "Mondo",
  "hasVip" : false,
  "location" : {
    "city" : "Madrid"
  }
}, {
  "_id" : "540b2ff281b30f3504a1c72f",
  "imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/540b2ff281b30f3504a1c72f_180.png",
  "name" : "Teatro Kapital",
  "hasVip" : false,
  "location" : {
    "address" : "Atocha, 125",
    "city" : "Madrid"
  }
}, {
  "_id" : "540cd44581b30f3504a1c73b",
  "imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/540cd44581b30f3504a1c73b_180.png",
  "name" : "Charada",
  "hasVip" : false,
  "location" : {
    "address" : "La Bola, 13",
    "city" : "Madrid"
  }
}]

通用響應集合序列化:

@objc public protocol ResponseCollectionSerializable {
    static func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [Self]
}

extension Alamofire.Request {
    public func responseCollection<T: ResponseCollectionSerializable>(completionHandler: (NSURLRequest, NSHTTPURLResponse?, [T]?, NSError?) -> Void) -> Self {
        let serializer: Serializer = { (request, response, data) in
            let JSONSerializer = Request.JSONResponseSerializer(options: .AllowFragments)
            let (JSON: AnyObject?, serializationError) = JSONSerializer(request, response, data)
            if response != nil && JSON != nil {
                return (T.collection(response: response!, representation: JSON!), nil)
            } else {
                return (nil, serializationError)
            }
        }

        return response(serializer: serializer, completionHandler: { (request, response, object, error) in
            completionHandler(request, response, object as? [T], error)
        })
    }
}

俱樂部對象類

final class Club: ResponseCollectionSerializable {

    @objc static func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [Club] {
        var clubs = [Club]()

        if let representation = representation as? [[String: AnyObject]] {
            for representationValue in representation {
                let club = Club(JSON: representationValue)
                clubs.append(club)
            }
        }

        return clubs
    }

    let id: String
    let name: String
    let imageUrl: String
    let hasVip: Bool
    let location: String

    init(JSON: AnyObject) {
        id = JSON.valueForKeyPath("id") as! String
        name = JSON.valueForKeyPath("name") as! String
        imageUrl = JSON.valueForKeyPath("imageUrl") as! String
        hasVip = JSON.valueForKeyPath("hasVip") as! Bool

        //is OK this implementation?
        location = JSON.valueForKeyPath("location") as! String
    }
}

視圖控制器類

class ClubsViewController: UIViewController, UITableViewDataSource{

    var results: [JSON]? = []
    var clubs: [Club]?

    @IBOutlet var tableview:UITableView!



    override func viewDidLoad() {
        super.viewDidLoad()
        self.loadClubsObjects()
    }

    func loadClubsObjects(){


        var URL = NSURL(string: "https://api.com/v1/clubs")
        var mutableURLRequest = NSMutableURLRequest(URL: URL!)
        mutableURLRequest.setValue("Content-Type", forHTTPHeaderField: "application/x-www-form-urlencoded")
        mutableURLRequest.HTTPMethod = "GET"
        mutableURLRequest.setValue("Bearer R01.iNsG3xjv/r1LDkhkGOANPv53xqUFDkPM0en5LIDxx875fBjdUZLn1jtUlKVJqVjsNwDe1Oqu2WuzjpaYbiWWhw==", forHTTPHeaderField: "Authorization")
        let manager = Alamofire.Manager.sharedInstance
        let request = manager.request(mutableURLRequest)
        request.responseCollection { (request, response, clubs: [Club]?, error) in

        println("request = \(request)")
        println("response = \(response)")
        println("clubs = \(clubs)")
        println("error = \(error)")

            if (json != nil){
                var jsonObj = JSON(json!)
                if let data = jsonObj["hits"].arrayValue as [JSON]? {
                    self.results = data
                    self.tableview.reloadData()

                }
            }
        }



    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.results?.count ?? 0
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("clubsObjectCell") as! ClubsTableViewCell
        cell.clubsObject = self.results?[indexPath.row]
        return cell    }

}

println(clubs)輸出為:

   request = <NSMutableURLRequest: 0x7fd553725870> { URL: https://api.com/v1/clubs }
response = Optional(<NSHTTPURLResponse: 0x7fd553439e20> { URL: https://api.com/v1/clubs } { status code: 200, headers {
    "Access-Control-Allow-Headers" = "X-Requested-With, Accept, Origin, Referer, User-Agent, Content-Type, Authorization";
    "Access-Control-Allow-Methods" = "GET,PUT,POST,DELETE,OPTIONS";
    "Access-Control-Allow-Origin" = "*";
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Tue, 21 Apr 2015 20:18:07 GMT";
    Etag = "W/\"sEDn5KBhpfpInjAtNsF4gQ==\"";
    Server = "nginx/1.6.2";
    "Transfer-Encoding" = Identity;
    Vary = "Accept-Encoding";
    "X-Powered-By" = Express;
} })
clubs = Optional([])
error = nil

為了確保您能將ViewController類的最后幾行更改為以下內容?

request.responseJSON { request, response, json, error in
    println(request)
    println(response)
    println(json)
    println(error)
}

我想確保您已經正確設置了請求,並且正在獲得期望的響應。 那肯定是成功的一半。 一旦您可以驗證,就可以處理responseCollection解析邏輯。

另外,您正在使用什么版本的Xcode和什么版本的Alamofire?


UPDATE

您遇到的問題有兩個方面。

第1期

首先,您沒有正確調用您的responseCollection方法。 您應該改為按以下方式調用它:

request.responseCollection { request, response, clubs: [Club], error in
    println("request = \(request)")
    println("response = \(response)")
    println("clubs = \(clubs)")
    println("error = \(error)")
}

然后,這將正確地進入您的Club班級。

第2期

第二個問題是您沒有在Club對象內部實現collection方法。 如果沒有真正遍歷整個系列,您將永遠不會得到任何俱樂部。 大致類似以下內容的內容可以使您朝正確的方向前進。

final class Club: ResponseCollectionSerializable {
    @objc static func collection(#response: NSHTTPURLResponse, representation: AnyObject) -> [Club] {
        var clubs = [Club]()

        if let representation = representation as? [[String: AnyObject]] {
            for representationValue in representation {
                let club = Club(JSON: representationValue)
                clubs.append(club)
            }
        }

        return clubs
    }

    let id: String
    let name: String
    let imageUrl: String
    let hasVip: Bool

    init(JSON: AnyObject) {
        id = JSON.valueForKeyPath("id") as! String
        name = JSON.valueForKeyPath("name") as! String
        imageUrl = JSON.valueForKeyPath("imageUrl") as! String
        hasVip = JSON.valueForKeyPath("hasVip") as! Bool
    }
}

一旦您的collection功能實際上遍歷了JSON數組中的所有表示形式的值,您應該會更加幸運。

獎勵積分

對於獎勵積分,這里有一些其他技巧可以改善您的代碼。

  • 切換到在Club類中使用失敗的初始化程序,以確保僅在JSON成功解析后才創建對象
  • responseCollection完成閉包中更改您的實現,以實際存儲新的Club值並在表視圖中顯示這些Club。

返回到responseCollection閉包的對象不再是可與SwiftyJSON一起使用的JSON AnyObject,而是一個Clubs數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM