Hi guys :slightly_smiling_face: We’re dipping our ...
# multiplatform
j
Hi guys 🙂 We’re dipping our toes in kotlin multiplaform and I’m kinda confused right now regarding “ios implementations” So we have something that look like that:
Copy code
interface StringNormalizer {

    fun normalizeString(stringWithAccents: String): String
}

expect fun getStringNormalizer(): StringNormalizer
and an easy Android implementation:
Copy code
class AndroidStringNormalizer : StringNormalizer {

    override fun normalizeString(stringWithAccents: String): String {
        val normalized = Normalizer.normalize(stringWithAccents, Normalizer.Form.NFD)
        return normalized.replace("\\p{Mn}+".toRegex(), "")
    }
}

actual fun getStringNormalizer(): StringNormalizer = AndroidStringNormalizer()
But I have no idea how to write the ios part:
Copy code
class IOSStringNormalizer : StringNormalizer {

    override fun normalizeString(stringWithAccents: String): String {
        TODO()
    }
}

actual fun getStringNormalizer(): StringNormalizer = IOSStringNormalizer()
I know the iOS team is currently using this string extension:
.folding(options: .diacriticInsensitive, locale: .current)
in Swift How can I have something equivalent in my
IOSStringNormalizer
? Thanks! 🙏
p
I don't think in iosMain you can access any single class from swift/objc the same as Java in androidMain. The interoperability with objc is a bit more limited to the packages that are already exposed. Now what you can do is export your interface to swift and implement it in swift. Then in iosMain create a function that receives a swift Callback that provides this interface when called from Kotlin. Could give you an example but far from keyboard now
m
It looks like
folding
is an instance function of
NSString
I think you need to cast the String to NSString, and then you can call the function.
stringByFoldingWithOptions
https://developer.apple.com/documentation/foundation/nsstring/1413779-stringbyfoldingwithoptions?language=objc
l
The Apple docs let you switch whether you see the swift or objc version. You can search for the Swift equivalent, then switch to the objc tab for the names. If there's no objc tab for a class, you'll need a swift wrapper.
👍 1
j
Hmmm, not sure I understand your suggestions guys 😞 What I’m looking for exists both in Swift
folding(options:locale:)
or objc
stringByFoldingWithOptions:locale:
I just don’t get how to use that I tried casting to NSString from kotlin but the IDE says the cast can never succeed Is there a way to make it work or is it not possible as @Pablichjenkov suggested?
hmmm, looks like the cast warning is a false positive and I can do that:
Copy code
@Suppress("CAST_NEVER_SUCCEEDS")
override fun normalizeString(stringWithAccents: String): String {
    return (stringWithAccents as NSString)
        .stringByFoldingWithOptions(NSDiacriticInsensitiveSearch, NSLocale.currentLocale())
}
m
Yeah, since String is final it shouldn't be castable, but on iOS is is actually a NSString so it works.
j
oh ok, makes sense 👍 thanks everyone for pointing me in the right direction 🙏