不能使用类型为“UInt32"的索引对类型为 [String] 的值进行下标; [英] Cannot subscript a value of type [String] with an index of type "UInt32"

查看:46
本文介绍了不能使用类型为“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

但是在让名称"行上,它给了我标题错误.有谁知道为什么以及如何解决它?

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天全站免登陆