【发布时间】:2021-03-29 04:45:10
【问题描述】:
我是 KMM 的新手,正在尝试使用带有 reified 的 ktor 创建一个用于 api 调用的通用函数,它似乎可以在 android 上正常工作,但在 iOS 中会引发错误 这是我在共享文件中常用的 api 调用返回。
@Throws(Exception::class)
suspend inline fun<reified T> post(url: String, requestBody: HashMap<String, Any>?) : Either<CustomException, T> {
try {
val response = httpClient.post<T> {
url(BASE_URL.plus(url))
contentType(ContentType.Any)
if (requestBody != null) {
body = requestBody
}
headers.remove("Content-Type")
headers {
append("Content-Type", "application/json")
append("Accept", "application/json")
append("Time-Zone", "+05:30")
append("App-Version", "1.0.0(0)")
append("Device-Type", "0")
}
}
return Success(response)
} catch(e: Exception) {
return Failure(e as CustomException)
}
}
如果我这样称呼它,它在 android 中效果很好:-
api.post<MyDataClassHere>(url = "url", getBody()).fold(
{
handleError(it)
},
{
Log.d("Success", it.toString())
}
)
但我无法让它在 iOS 设备上运行,它显示如下错误:-
some : Error Domain=KotlinException Code=0 "unsupported call of reified inlined function `com.example.myapplication.shared.apicalls.SpaceXApi.post`" UserInfo={NSLocalizedDescription=unsupported call of reified inlined function `com.example.myapplication.shared.apicalls.SpaceXApi.post`, KotlinException=kotlin.IllegalStateException: unsupported call of reified inlined function `com.example.myapplication.shared.apicalls.SpaceXApi.post`, KotlinExceptionOrigin=}
对此的任何帮助表示赞赏。谢谢
【问题讨论】:
标签: android ios generics kotlin-multiplatform ktor