扩展 属性: 函数声明必须有一个名称 未解析的引用: get
Extension property: Function declaration must have a name Unresolved reference: get
我使用扩展函数来扩展retrofit2.Response
对象:
片段:
public class ErrorResponse {
private int code;
private String message;
private Response response;
}
import okhttp3.MediaType
import okhttp3.Protocol
import okhttp3.Request
import okhttp3.ResponseBody
import retrofit2.Response
fun Response<*>.errorResponse(): ErrorResponse {
val errorResponse = ErrorUtils.parseError(this)
return errorResponse
}
这里使用:
viewModelScope.launch(Dispatchers.Main) {
val response: Response<*> = TransportService.getTraidersList()
if (response.isSuccessful) {
finishLoadData()
val traders: List<Trader> = response.body() as List<Trader>
traderListLiveData.postValue(traders)
} else {
val errorResponse = response.errorResponse()
val message = errorResponse.message // here use extension function
messageLiveData.value = SingleEvent(message)
}
}
不错。工作正常。
但我想使用扩展属性。我试试这个:
val Response<*>.errorResponse: ErrorResponse {
get() = ErrorUtils.parseError(this)
}
但是我得到编译错误:
Function declaration must have a name Unresolved reference: get
属性不需要括号。它可能看起来像这样:
val Response<*>.errorResponse: ErrorResponse
get() = ErrorUtils.parseError(this)
我使用扩展函数来扩展retrofit2.Response
对象:
片段:
public class ErrorResponse {
private int code;
private String message;
private Response response;
}
import okhttp3.MediaType
import okhttp3.Protocol
import okhttp3.Request
import okhttp3.ResponseBody
import retrofit2.Response
fun Response<*>.errorResponse(): ErrorResponse {
val errorResponse = ErrorUtils.parseError(this)
return errorResponse
}
这里使用:
viewModelScope.launch(Dispatchers.Main) {
val response: Response<*> = TransportService.getTraidersList()
if (response.isSuccessful) {
finishLoadData()
val traders: List<Trader> = response.body() as List<Trader>
traderListLiveData.postValue(traders)
} else {
val errorResponse = response.errorResponse()
val message = errorResponse.message // here use extension function
messageLiveData.value = SingleEvent(message)
}
}
不错。工作正常。
但我想使用扩展属性。我试试这个:
val Response<*>.errorResponse: ErrorResponse {
get() = ErrorUtils.parseError(this)
}
但是我得到编译错误:
Function declaration must have a name Unresolved reference: get
属性不需要括号。它可能看起来像这样:
val Response<*>.errorResponse: ErrorResponse
get() = ErrorUtils.parseError(this)