2016-12-27 6 views
0

私はAlamofireObjectMapper拡張を使用します。私は応答を得たいですが、失敗しました。レスポンスクロージャのコードでは、response.result.isSuccess { tableLeague = response.result.value }が呼び出されない場合、 で{{(response:DataResponse)}を呼び出さないため、私のtableLeagueオブジェクトは常にnilです。Alamofire.request()。responseObjectは応答を返しません

let header = ["X-Auth-Token":"1231231"] 

static let sharedInstance = ServerAPI() 
private func getRequest(uri: String) -> DataRequest { 
    return Alamofire.request(uri, method: .get, headers: header) 
} 

public func getTableLeague() -> TableLeague { 
    var tableLeague: TableLeague? 
    getRequest(uri: URL).responseObject { (response: DataResponse<TableLeague>) in 
     if response.result.isSuccess { 
      tableLeague = response.result.value 
     } 
    } 
    return tableLeague! 
} 

とビジネスクラスで使用します:私は次のメソッドを使用

public func readTableLeague() -> TableLeague { 
    let tableLeague = ServerAPI.sharedInstance.getTableLeague() 
    return tableLeague 
} 

を私は、応答がまだ持っていないので、それができると思いますが、私はまだ

を持っていないオブジェクトを設定しよう

何が問題ですか?完了ハンドラ私は他を使用する必要がありますか?

+0

でJSONレスポンスシンプルなマップを取得すると、あなたの問題についてもう少し説明を追加してください

class Response: Mappable { var success: Bool? var data: [Data]? required init?(_ map: Map) { mapping(map) } func mapping(map: Map) { success <- map["success"] data <- map["data"] } } class Data: Mappable { var uid: Int? var name: String? // add other field which you want to map required init?(_ map: Map) { mapping(map) } func mapping(map: Map) { uid <- map["uid"] name <- map["name"] } } 

次の構造によってマッピングしてみてください。 –

+0

私はより多くの説明を追加します。 –

+0

これは、MapperクラスがレスポンスをあなたのTableLeagueクラスにマッピングしないことを意味します。 JSONオブジェクトを含むレスポンスオブジェクトを確認してください。 –

答えて

0

この構文

let response = Mapper<Response>().map(responseObject) 
if let id = response?.data?[0].uid { 
    println(id) 
} 
関連する問題