Hi all! From Kotlin Native I’m trying to access a ...
# kotlin-native
j
Hi all! From Kotlin Native I’m trying to access a swift extension:
Copy code
SWIFT CODE:
let originalString = "test/test"
let escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
print(escapedString!)
I found that this function is defined as an extension in a class called
StringProtocol
Copy code
extension StringProtocol where Self.Index == String.Index {
Any idea how can I get access to this function from Kotlin Native? Thanks!
I didn’t find a way to access this function but yes this other one and it works!:
Copy code
fun String.encode(): String? {
    return (this as? NSString)?.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet)
}
Still I’m not totally sure if this cast to NSString is the right way to go as the IDE warns this will never work:
This cast can never success
s
This cast can never success
Known issue, this warning is wrong. Your Kotlin function definition is fine.
j
Thanks @svyatoslav.scherbina! And do you know how can I access a swift extension?
s
You can’t access pure Swift extension. On the other hand, this function doesn’t seem to be Swift extension, it is the same as
stringByAddingPercentEncodingWithAllowedCharacters
. You can ensure this by switching language between Swift and Objective-C in the documentation for one of these functions.
j
In my original question I asked about this function:
Copy code
originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
I end up using the other one that you mentioned as an alternative
ohh my bad.. now I know what you mean! Interesting, thanks for pointing this out!