无法使用索引类型为"UInt32"的下标来为类型为[String]的值下标. [英] Cannot subscript a value of type [String] with an index of type "UInt32"

查看:91
本文介绍了无法使用索引类型为"UInt32"的下标来为类型为[String]的值下标.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建代码以生成随机名称.在这里

i've been trying to create a code to generate random names. Here it is

import UIKit

 let arrayOfNames: [String] = ["Giovanni", "Simone", "Francesco", "Ahmet",       "Valerio", "Federico"]

 let arrayOfsNames: [String] = [" İl Genio", " Lo scemo", " Verga", " Fermi", " Medici", " L'assasino"]

 var casual1 = arc4random_uniform(7)
 var casual2 = arc4random_uniform(7)

 let name = "\(arrayOfNames[casual1]) + \(arrayOfsNames[casual2])"

  name

但是在"let name"行上,它给我标题上的错误.有谁知道为什么以及如何解决这个问题?

however on the "let name" line it gives me the mistake on the title. Does anyone know why and how to solve it?

推荐答案

您应该使用Int通过索引访问数组

You should use an Int to access an array by index

替换此

var casual1 = arc4random_uniform(7)
var casual2 = arc4random_uniform(7)

与此

var casual1 = Int(arc4random_uniform(7))
var casual2 = Int(arc4random_uniform(7))

这篇关于无法使用索引类型为"UInt32"的下标来为类型为[String]的值下标.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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