[英]Cannot subscript a value of type [String] with an index of type “UInt32”
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?
有谁知道为什么以及如何解决它?
You should use an Int
to access an array by index您应该使用
Int
按索引访问数组
Replace this替换这个
var casual1 = arc4random_uniform(7)
var casual2 = arc4random_uniform(7)
with this有了这个
var casual1 = Int(arc4random_uniform(7))
var casual2 = Int(arc4random_uniform(7))
Swift 4.2 implemented SE-0202: Random Unification Thus there is no need to use the imported C function arc4random(). Swift 4.2 实现了SE-0202:随机统一因此不需要使用导入的 C 函数 arc4random()。 You can now use Swift's own native random API.
您现在可以使用 Swift 自己的原生随机 API。 By calling the
random()
method on any numeric type with the range needed通过对具有所需范围的任何数字类型调用
random()
方法
Thus因此
var casual1 = arc4random_uniform(7)
var casual2 = arc4random_uniform(7)
should be written as.应该写成。
var casual1 = Int.random(in: arrayOfNames.indices)
var casual2 = Int.random(in: arrayOfsNames.indices)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.