dambakk
11/23/2020, 8:47 AMsealed class Response<out T : Any> {
data class Success<out T : Any>(val data: T) : Response<T>()
data class Error(val reason: ErrorReason) : Response<Nothing>()
}
Receiving success in ios is ok. However, we are not able to cast the result to an Response.Error . When printing the object to console ((lldb) e dump(response)) we can see that it is an Optional(Error) object with all the params we are looking for, but we cannot cast it to en Error in any way. Anyone knows what we have to do?dambakk
11/23/2020, 9:46 AMlouiscad
11/23/2020, 10:52 AMOption(Error) looks like the NSError? type, which cannot be cast to your custom typelouiscad
11/23/2020, 10:52 AMErrorReason?dambakk
11/23/2020, 11:03 AMsealed class ErrorReason(override val message: String) : Throwable(message) {
object TokenExpired : ErrorReason("Token expired")
object MissingToken : ErrorReason("Missing token")
...
}louiscad
11/23/2020, 11:05 AMHowever, we are not able to cast the result to anIt's unclear what you're trying to cast… Do you know?.Response.Error
dambakk
11/23/2020, 11:05 AMdambakk
11/23/2020, 11:14 AMapi.someSuspendMethod(params...) { result, error in
}
where result could be a Result.Success or Result.Error. Makes sense? I’m not an ios dev, but as I have understood result could have a value or error can have a value, hence the Option wrapper. (Tho the errors will be in the result variables in this case).
It seems like the result we receive is of type Result.Success even though its a Result.Error which we can see when printing the result to the console. So we want to cast the result variable to Success if it is a success or an Error if it is an error.
someSuspendMethod is defined as something like this:
suspend fun someSuspendMethod(params): Result<ResponseType> { ... }
Is anything else unclear?dambakk
11/23/2020, 11:14 AMlouiscad
11/23/2020, 1:09 PMResult.Error or Response.Error? Juding from that, it seems that your issue takes its root in not knowing what type is exposed/used exactly in the API.dambakk
11/23/2020, 3:21 PMkotlinNothing was not ResponseType. So changing the Response class from the question to this:
sealed class Response<out T : Any> {
data class Success<out T : Any>(val data: T) : Response<T>()
data class Error<out T : Any>(val reason: ErrorReason) : Response<T>()
}
did the trick.
Thansks for your time 🙌