简体   繁体   English

kotlin 中的字符串格式和可变参数

[英]String format and vararg in kotlin

I have the following method我有以下方法

fun formatMessages(indicators: IntArray): CharSequence {
    return context.getString(R.string.foo, indicators)
}

And the string is:字符串是:

<string name="foo">$1%d - $2%d range of difference</string>

I get a complaint from Android Studio that:我收到来自 Android Studio 的投诉:
Wrong argument count, format string requires 2 but format call supplies 1

What I am really trying to accomplish is to be able to pass to such a formatMessages any number of indicators (1,2,3..) and the proper string would be selected/displayed.我真正想要完成的是能够将任意数量的指标(1,2,3..)传递给这样的formatMessages ,并且将选择/显示正确的字符串。

Modify your function to this:将您的 function 修改为:

fun formatMessages(indicators: IntArray): CharSequence {
    return context.getString(R.string.foo, indicators[0], indicators[1])
}

But of course you need a proper checking that indicators length is at least 2 so it will not crash.但当然,您需要适当检查指标长度是否至少为 2,这样它就不会崩溃。

Reason for this is getString(int resId, Object... formatArgs) runtime will fail because it expects 2 parameters from what is defined in the string resource.原因是getString(int resId, Object... formatArgs)运行时将失败,因为它需要字符串资源中定义的 2 个参数。

When we call a vararg-function, we can pass arguments one-by-one, eg asList(1, 2, 3), or, if we already have an array and want to pass its contents to the function, we use the spread operator (prefix the array with *): 当我们调用一个可变参数函数时,我们可以一对一地传递 arguments,例如 asList(1, 2, 3),或者,如果我们已经有一个数组并且想要将其内容传递给 function,我们使用扩展运算符(以 * 为数组前缀):

fun formatMessages(indicators: Array<Object>): CharSequence {
    return context.getString(R.string.foo, *indicators)
}

If you need indicators to have type IntArray , you'll have to convert it:如果您需要indicators的类型为IntArray ,则必须对其进行转换:

fun formatMessages(indicators: IntArray): CharSequence {
    return context.getString(R.string.foo, *(Array<Object>(indicators.size) { indicators[it] }))
}

kotlin 参考 function 带可变参数并转换数组<out to list<out< div><div id="text_translate"><p> 我已经根据这个主题<a href="https://stackoverflow.com/questions/62260177/kotlin-geneirc-with-vararg-parameter" rel="nofollow noreferrer">基本问题提出了问题</a></p><p>所以,我想提前问一下。 有人用数组和列表回答了这个问题</p><pre>Class Test&lt;T,V&gt;{ var functionPara:(()-&gt;T)? = null var recallFunctionWithFunction:( (Array&lt;out T&gt;) -&gt; V)? = null constructor(value: ()-&gt;T, recallFunctionWithFunction: (Array&lt;out T&gt;) -&gt; V ){ this.functionPara = value this.recallFunctionWithFunction = recallFunctionWithFunction } inline fun &lt;reified T, V&gt; compose(crossinline f: (Array&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results.toTypedArray()) } } fun &lt;T, V&gt; compose(f: (List&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results) } } } fun runCompose(){ compose(functionPara,recallFunctionWithFunction).invoke() }</pre><p> 但我发现,当我引用带有 vararg 参数的 function 时</p><pre>fun functionA(vararg:Observable&lt;Any&gt;):LiveData&lt;Boolean&gt;{ } fun functionB():Observable&lt;Any&gt;{ }</pre><p> 当我执行类似::functionA 的操作时,A 的类型将是Array&lt;out Observable&lt;Any&gt;&gt;-&gt;LiveData&lt;Boolean&gt;因此,当我执行类似操作时</p><p>Test&lt;Observable&lt;Any&gt;,LiveData&lt;Boolean&gt;&gt;(::functionB,::functionA).runCompose()</p><p> <strong><em>情况 1</em></strong>如果我使用 compose function 并接受 List 类型,它将显示类型不匹配,因为引用::functionA 将返回 Array</p><p><img src="https://i.stack.imgur.com/X3k3A.png" alt="图片1"></p><p> <strong><em>情况 2</em></strong>如果我使用 compose function 并接受 Array 类型,它将显示错误</p><p>不能使用“T”作为具体类型参数。 改用 class</p><p><img src="https://i.stack.imgur.com/Z2iwJ.png" alt="图片2"></p><p> 在上一篇文章中,有人回答我将数组转换为列表。 但是如何将引用 function 与 vararg 参数与原始Array&lt;out to List &lt;out ? 当我引用 function 之类的 function 时,类型必须是Array&lt;out但我想将其插入到 compose function 中。 我必须转换它。 任何人都可以帮忙吗? 我在那儿呆了很长时间。 希望有人能救我!!</p></div></out> - kotlin reference function with vararg parameter and convert Array<out to List<out

暂无
暂无

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

相关问题 kotlin 中的 vararg 没用吗? - Is vararg in kotlin useless? 引用vararg函数参数Kotlin - Referring to vararg function parameters Kotlin Kotlin-vararg-&gt;更改查询参数值 - Kotlin - vararg -> change query param values 不同格式的字符串到日期 kotlin - different format String to date kotlin 将vararg参数传递给Kotlin中的另一个函数时编译时间错误 - Compile time error in passing vararg parameter to another function in Kotlin 如何在Kotlin的枚举中检索用vararg声明的参数的值 - How to retrieve the value(s) of a parameter declared with vararg in an enum in Kotlin 如何在 Kotlin 中将字符串转换为特定格式? - How to cast a string to a specific format in Kotlin? kotlin 参考 function 带可变参数并转换数组<out to list<out< div><div id="text_translate"><p> 我已经根据这个主题<a href="https://stackoverflow.com/questions/62260177/kotlin-geneirc-with-vararg-parameter" rel="nofollow noreferrer">基本问题提出了问题</a></p><p>所以,我想提前问一下。 有人用数组和列表回答了这个问题</p><pre>Class Test&lt;T,V&gt;{ var functionPara:(()-&gt;T)? = null var recallFunctionWithFunction:( (Array&lt;out T&gt;) -&gt; V)? = null constructor(value: ()-&gt;T, recallFunctionWithFunction: (Array&lt;out T&gt;) -&gt; V ){ this.functionPara = value this.recallFunctionWithFunction = recallFunctionWithFunction } inline fun &lt;reified T, V&gt; compose(crossinline f: (Array&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results.toTypedArray()) } } fun &lt;T, V&gt; compose(f: (List&lt;out T&gt;) -&gt; V, vararg g: () -&gt; T): () -&gt; V { val results = g.map { it() } return { f(results) } } } fun runCompose(){ compose(functionPara,recallFunctionWithFunction).invoke() }</pre><p> 但我发现,当我引用带有 vararg 参数的 function 时</p><pre>fun functionA(vararg:Observable&lt;Any&gt;):LiveData&lt;Boolean&gt;{ } fun functionB():Observable&lt;Any&gt;{ }</pre><p> 当我执行类似::functionA 的操作时,A 的类型将是Array&lt;out Observable&lt;Any&gt;&gt;-&gt;LiveData&lt;Boolean&gt;因此,当我执行类似操作时</p><p>Test&lt;Observable&lt;Any&gt;,LiveData&lt;Boolean&gt;&gt;(::functionB,::functionA).runCompose()</p><p> <strong><em>情况 1</em></strong>如果我使用 compose function 并接受 List 类型,它将显示类型不匹配,因为引用::functionA 将返回 Array</p><p><img src="https://i.stack.imgur.com/X3k3A.png" alt="图片1"></p><p> <strong><em>情况 2</em></strong>如果我使用 compose function 并接受 Array 类型,它将显示错误</p><p>不能使用“T”作为具体类型参数。 改用 class</p><p><img src="https://i.stack.imgur.com/Z2iwJ.png" alt="图片2"></p><p> 在上一篇文章中,有人回答我将数组转换为列表。 但是如何将引用 function 与 vararg 参数与原始Array&lt;out to List &lt;out ? 当我引用 function 之类的 function 时,类型必须是Array&lt;out但我想将其插入到 compose function 中。 我必须转换它。 任何人都可以帮忙吗? 我在那儿呆了很长时间。 希望有人能救我!!</p></div></out> - kotlin reference function with vararg parameter and convert Array<out to List<out Kotlin:如何将 LocalDateTime 列表格式化为字符串? - Kotlin: How to format a list of LocalDateTime to String? 检查 Kotlin 中字符串格式的正确方法是什么? - What is the correct way of checking the format of a String in Kotlin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM