我需要通过 Kotlin 的 Pair
作为论据。
<argument android:name="data" app:argType="kotlin.Pair" />
e: CreateCodeFragmentDirections.kt: (16, 15): 2 type arguments expected for class Pair<out A, out B>
<argument android:name="data" app:argType="kotlin.Pair<String, String>" />
Cause: markup not allowed inside attribute value - illegal < (position: TEXT seen ...<argument android:name="data" app:argType="kotlin.Pair<... @132:30)
<argument android:name="data" app:argType="kotlin.Pair<String, String>" />
Can't escape identifier `Pair<String, String>` because it contains illegal characters: <>
以上尝试均未奏效。我该怎么做?
最佳答案
我认为不可能使用泛型类型参数,因此您必须像第一个案例一样回退到原始泛型。我正在使用 java,它编译得很好,但是我认为你的不是,因为 Kotlin 不允许原始的泛型使用。
实际上,我发现使用带有单个类型参数的最后一种方法,生成的代码包含可怕的错误。最后,在我看来,泛型类型根本不支持,也许在将来......最好请求该功能,同时您可以尝试分离参数(如果适用)或使用非泛型 super type (Object ?? --it must be Parcelable/Serializable) with casts,或者您可能想创建自己的非泛型 Pair 类并仅转换组件或特定的“StringStringPair”或将所有参数分组为单个非泛型键入...或切换到 java。
关于android - 如何使用 Safe Args 传递通用数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58394262/