簡體   English   中英

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

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

我寫 function

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

並得到這個錯誤。 我認為值字符串並與 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是 KVO 方法,與用戶默認值無關。

將其替換為setObject

NSUserDefaults.standardUserDefaults.setObject(value, key)

ps 如果您需要使用setValue否則您將面臨與其他方法相同的問題,您可以通過提供第二個參數名稱來指定所需的方法,例如setValue(value, forKeyPath = key)

除了上述答案之外,如果您正在尋找即插即用的 KMP 鍵值存儲,多平台設置值得一試。 它還在底層使用NSUserDefaults

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM