In my MPP app I'm trying to create an `NSDecimal` ...
# multiplatform
k
In my MPP app I'm trying to create an
NSDecimal
from a
String
. However I'm not sure how to as the
NSDecimal
constructor only takes a
NativePtr
argument. Can someone tell me how to do this please? 😄
r
There’s a bunch of different constructors, so you might need to specify a parameter name to make sure you’re using the right one. Eg
val nsDecimal = NSDeclmal(string = "123.456")
k
Thanks @russhwolf. However no luck
Given in Swift you can construct a
Decimal
from a string, I'm very confused here ...
e
Kotlin/Native interop is at the Objective-C level, not Swift, so you should do the same thing you do in Objective-C: work with
NSDecimalNumber
and use
.decimalNumber
to get a Swift-compatible
Decimal
as needed
k
Thanks @ephemient. Getting closer. However
.decimalNumber
returns a
CValue<NSDecimal>
I'm trying to create a function that returns a
NSDecimal
and the compiler doesn't like returning a
CValue<NSDecimal>
Copy code
type kotlinx.cinterop.CValue<platform.Foundation.NSDecimal>  of return value is not supported here: not a structure or too complex
k
Copy code
fun foo(str: String): NSDecimal = NSDecimal(NSDecimalNumber(string = str).objcPtr())
does it work?
k
@Konstantin Tskhovrebov Yes it did. Thanks.
@Konstantin Tskhovrebov Now I just have to figure out why my tests are segfaulting when running on iOS (https://kotlinlang.slack.com/archives/C3PQML5NU/p1650596218842939)