Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

arrays - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should use an Int to access an array by index

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...