I've got a line of code like this:
val smsRetrieverStatus = extrasObj?.get(SmsRetriever.EXTRA_STATUS) as Status
But it's now deprecated as shown:
Is there any alternative way of doing this without getting deprecation warnings?
If you read the method documentation here, it says:
This method was deprecated in API level 33. Use the type-safe specific
APIs depending on the type of the item to be retrieved, eg.
getString(java.lang.String)
So, they're telling you to stop using the generic get() method, and instead use a type-specific one -- getString(), getInt(), getLong(), etc.
Update:
If you have a custom class or data type, then you'll need to implement your own logic to serialize that class (maybe use something like Gson). And once you've serialized it into a String, you can then use getString()
You can try another method
val status = extrasObj?.getParcelable(SmsRetriever.EXTRA_STATUS, Status::class.java)
Note that this method is not stable and sometimes throws NPE. So you'd better catch and handle the NPE.
Related
This might be a very silly question, but I am logging the methods that are triggered in my app as strings. When an issue is submitted, I would like to automatically input the text of the strings as parameters for methods. E.g:
For method:
fun assignPot(potType: PotType, ball: DomainBall, action: PotAction) {...}
I'd like to somehow call method:
assignPot(FOUL(2, BLUE(5), SWITCH))
From String:
"FOUL(2, BLUE(5), SWITCH)"
The only workaround I can think of is to split the string and create a when -> then function to get actual classes from strings, but I wondered if there's a more concise way for this.
This is not what you want to do. You should design your app in a way that prevents users from providing input similar to actual code.
However, you can achieve this. Complex parsings like this oftenly use regex-based approaches.
As you said, you should map your string part to class. If your PotType is enum, you can do something like
val re = Regex("[^A-Za-z\s]")
val reNumbers = Regex("[^0-9\s]")
// get classes
val classNames = originalString.replace(re, "").split(" ")
// get their constructor numerical arguments
val classArgs = originalString.replace(reNumbers, "").split(" ")
After that you can implement mapping with when expression. You probably will use some collection of Any type.
As you see, this sadly leads you to parsing code by code. Concise way to solve is to implement your own script compiler/interpreter and use it in your application :) That will later lead you to dealing with security breaches and so on.
If you are logging problematic method calls and want to repeat them immediately after issue is submitted, you probably want to programatically save the calls to lambdas and call them when you receive an issue log.
I am using GraphQL Apollo client call and it generates files. So as a result I got this
val storeNumber: Input<String> = Input.absent()
Instead of regular string. So how can I cast parameter to Input<String> to avoid this error
I don't use Apollo, but found the source code of Input. It depends what version of this library you're using. If you're using an older version, to wrap (not "cast"!) a String as an Input, use Input.Present:
storeNumber = Input.Present(storeNumber)
Note, the term "cast" means promising the compiler that your existing instance is also an instance of something else. That is very different from converting or wrapping an instance of something.
If you're using a newer version of the library, you shouldn't be using the Input class at all. It's been replaced with the Optional class, in which case you would use Optional.Present(storeNumber).
If you want to figure this kind of thing out on your own in the future, try Ctrl+Clicking the function you're working with to jump to its source code. In turn you can Ctrl+Click the types of the function parameters. That would take you to the source code of Input so you could see how to create an instance.
I have started working with a Kotlin Multiplatform library, and I make use of Gson on the android side to do some processes. Since I know iOS does not have support for Gson, I decided to create an expected function called convertToJson which has a return type of a class. However when I have the actual implementation, I get the following error:
None of the arguments can be called with the arguments supplied
This occurs at this line of code:
actual fun convertFromJson(json:String): ClassName {
return Gson().fromJson(json, ClassName::class) // this is where the error lies
}
I also have an actual implementation of a function called convertToJson which does not give me an error, so I am wondering why the above mentioned convertFromJson function gives me an error.
Any help or advice will be highly appreciated.
Gson is implemented in java and its fromJson accepts an instance of java.lang.Class as the argument, so you should replace ClassName::class with ClassName::class.java, because the former gives you an instance of kotlin.reflect.KClass.
I am learning how to use the coroutines in kotlin. looking at some examples in the internet i found that within the context f the also operator the reference
it
is used. i could not find any explanation about the meaning of
it
please provide some brief explanantion about what does "it" mean
when you use the also method, it has 1 parameter.
Think of it in Java kinda like this:
foo.also(int it) {
// do stuff
}
In Kotlin, the it parameter is implicit (sometimes you might want to use it sometimes you don't).
If you want to rename it to something more readable you can
foo.also { newName ->
// do stuff with newName
}
Or just use it like it is
foo.also {
// do stuff with $it
}
So therefore when you are using a method (or a closure/lambda) if it has 1 parameter, then the implicit name of that parameter is always it.
Basically it represents the lambda parameter
let's say you want to perform anything on the variable but do to check the nullity first, you can do it like
var str:String?=null // str is of string type
now you can use it fail safe
str?.let{it:String// youll see like this
// now you can access str as **it**
}
it is the implicit name of a single parameter
For more information about it and this in scoping functions like also
I am using android studio with Kotlin language
and when I call the function above, its warning to check null state of "obj"
How its possible to make IDE (Android Studio) to understand that after some method call, the (passed) object exactly not null... thanks
Instead of an opaque isNotNull method call, which Kotlin can't (and shouldn't) reason about, use one of the idioms native to the language, such as:
value?.let {
val response = it.response
...
}