简体   繁体   English

如何处理 Kotlin function Android 中的简单错误

[英]How to handle simple error in Kotlin function Android

I'm having trouble handling error in the following function. I'm basically new to Kotlin. Here's my RevenueCat Login Code and I want to handle::error in this code:我在处理以下 function 中的错误时遇到问题。我基本上是 Kotlin 的新手。这是我的 RevenueCat 登录代码,我想在此代码中处理::错误:

Purchases.sharedInstance.logInWith(
                    myUserID,
                    ::error // <- How to handle this? I want to retrieve error Code and Error Message.
            )
{ customerInfo, created  ->

// Handle Successful login here

}
          

Here's the code behind the function (within RevenueCat SDK)这是 function 背后的代码(在 RevenueCat SDK 中)

@Suppress("unused")
fun Purchases.logInWith(
    appUserID: String,
    onError: (error: PurchasesError) -> Unit = ON_ERROR_STUB,
    onSuccess: (customerInfo: CustomerInfo, created: Boolean) -> Unit
) {
    logIn(appUserID, logInSuccessListener(onSuccess, onError))
}

The double colon in ::error is afunction reference . ::error中的双冒号是一个function 引用 It is basically a reference to the function error() .它基本上是对 function error()的引用。

And from your logInWith() function, we have onError: (error: PurchasesError) -> Unit = ON_ERROR_STUB , meaning that the function should take PurchasesError as input parameter and does not need to return.从你的logInWith() function,我们有onError: (error: PurchasesError) -> Unit = ON_ERROR_STUB ,这意味着 function 应该将PurchasesError作为输入参数并且不需要返回。

So we can derive a function as the following:所以我们可以推导出一个function如下:

fun error(error: PurchasesError) {
    // And you can do something with the error here
}

I solved it like this:我这样解决了:

Purchases.sharedInstance.logInWith(
     myUserID, 
 onError = { error -> 
        
        // Handle error here 
        
        }

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

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