繁体   English   中英

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

[英]SWIFT IOS return 2 different objects in a same function

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

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)

您可以使用如下的Tuple返回两个不同的对象:

//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

暂无
暂无

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

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