SWIFT IOS在同一个函数中返回2个不同的对象

时间:2015-01-30 13:04:40

标签: ios iphone swift return afnetworking

如果一切顺利,我想返回请求,如果出现问题则返回错误。

func afnwPost(url: String,client_id: String, client_secret:String,grant_type:String, userparam: String, passwordparam:String)-> AnyObject{

    var parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":grant_type,"username":userparam,"password":passwordparam]

    manager.POST( url,
    parameters: parameters,
    success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
    println("JSON: " + responseObject.description)
    return responseObject
    },
    failure: { (operation: AFHTTPRequestOperation!,error: NSError!) in
    println("Error: " + error.localizedDescription)
    return error
    })
}

通话方法

 var respuesta = obtenerToken.afnwPost(host_oauth,
        client_id: client_id,
        client_secret:client_secret,
        grant_type:grant_type_token,
        userparam: textfieldUsuario.text,
        passwordparam: textfieldContrasenya.text)

1 个答案:

答案 0 :(得分:3)

您可以使用元组返回两个不同的对象:

//Dummy method to show how it works.
func yourFunction()->(responseObject:AnyObject, error:String){
  return (responseObject, error)
}

要再次访问它们,请执行以下操作:

var yourResult = yourFunction()
println(yourResult.responseObject)
println(yourResult.error)

当然,您必须检查,当您收到函数的回复时,该值为nil