简体   繁体   English

Kotlin 多平台使用 iOS 上的 NSUserDefaults setValue String 出错

[英]Kotlin Multiplatform use NSUserDefaults setValue String on iOS got error

I write function我写 function

    actual fun setItem(key: String, value: String) {
        NSUserDefaults.standardUserDefaults.setValue(value, key)
    }

And got this error.并得到这个错误。 I think value string and use with Any我认为值字符串并与 Any 一起使用

Showing Recent Messages
The following Kotlin source sets were configured but not added to any Kotlin compilation:

 * androidAndroidTestRelease

 * androidTestFixtures

 * androidTestFixturesDebug

 * androidTestFixturesRelease

You can add a source set to a target's compilation by connecting it with the compilation's default source set using 'dependsOn'.

See https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#connecting-source-sets



> Task :shared:compileKotlinIosX64 FAILED

e: /Users/anh.nguyen25/Desktop/momo-app-vix/shared/src/iosMain/kotlin/vn/momo/core/modules/storage/FastStorage.kt: (14, 45): Overload resolution ambiguity: 

public external fun NSObject.setValue(value: Any?, forKey: String): Unit defined in platform.Foundation

public external fun NSObject.setValue(value: Any?, forKeyPath: String): Unit defined in platform.Foundation

public external fun NSObject.setValue(value: Any?, forUndefinedKey: String): Unit defined in platform.Foundation



FAILURE: Build failed with an exception.



* What went wrong:

Execution failed for task ':shared:compileKotlinIosX64'.

> Compilation finished with errors

setValue is KVO method which has nothing to do with user defaults. setValue是 KVO 方法,与用户默认值无关。

Replace it with setObject :将其替换为setObject

NSUserDefaults.standardUserDefaults.setObject(value, key)

ps if you need to use setValue or you'll face same problem with other methods, you can specify the needed method by providing the second parameter name, eg setValue(value, forKeyPath = key) ps 如果您需要使用setValue否则您将面临与其他方法相同的问题,您可以通过提供第二个参数名称来指定所需的方法,例如setValue(value, forKeyPath = key)

In addition to the answer above, if you're looking for a plug-and- play KMP key-value store, Multiplatform Settings is worth checking out.除了上述答案之外,如果您正在寻找即插即用的 KMP 键值存储,多平台设置值得一试。 It also uses NSUserDefaults under the hood.它还在底层使用NSUserDefaults

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM