我想在多个类中使用此函数:
fun <T> T?.ifNull(function: (T?, s:String) -> Unit) {
}
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
这就是我想用它的方式:
class A{
fun <T> T?.ifNull(function: (T?, s:String) -> Unit) {
}
}
class B{
constructor(){
val a = A()
//I want to use the function here
}}
Run Code Online (Sandbox Code Playgroud) 我怎样才能做到这一点?这是我在做的请求。
Fuel.get(url)
.timeout(TIMEOUT)
.body(myRequest.parameters!!, Charsets.UTF_8)
.responseString() { _, response, result ->
Log.e("result", "$result")
when (result) {
is Result.Success -> {
completionHandler(result.get(), null)
}
is Result.Failure -> {
Log.e("fail", "${response}")
completionHandler(null, Error(response.responseMessage))
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的回应是:
Response : Bad Request
Length : 122
Body : ({"message":"You have already booked that iceCream"})
Headers : (9)
Connection : [keep-alive]
Content-Length : [122]
Content-Type : [application/json; charset=utf-8]
Date : [Thu, 03 May 2018 07:33:21 GMT]
Server : [nginx/1.12.1]
Run Code Online (Sandbox Code Playgroud)
我想将正文消息作为我的错误返回。