在其他数组中使用 ForEach 的索引 [英] Using index from ForEach in other array

查看:31
本文介绍了在其他数组中使用 ForEach 的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能使用 FromEach 的索引作为其他数组的索引.这个索引是Int,有什么问题吗?

Why I can't use index from FromEach as index for other array. This index is Int, so what's the problem?

var word: String{
    return slova[selector]
}

var symbols: Array<Character>{
    return [Character](word)
}

var body: some View {
    HStack{
        ForEach(0..<word.count-1){index in
            Button("\(symbols[index])") {

            }

        }

    }
}

result: bug in "Button("(symbols[index])") {" :实例方法'appendInterpolation'要求'Character'符合'_FormatSpecifiable'

result: bug in "Button("(symbols[index])") {" :Instance method 'appendInterpolation' requires that 'Character' conform to '_FormatSpecifiable'

推荐答案

错误不是由于索引,而是关于字符串生成,改用

The error is not due index, but about string generation, use instead

    ForEach(0..<word.count-1){index in
        Button(String(self.symbols[index])) { // << here !

这篇关于在其他数组中使用 ForEach 的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆