Answer the question
In order to leave comments, you need to log in
Swift: how to access a random element of a two-dimensional array?
I set an array of arrays (2D array): var multArray = Array<Array<String>>()
I carried out the procedure of its "initialization" that was not completely clear to me, without which an error is generated:
for i in 0..<firstBlockStruct.count {
multArray.append(Array(count:0, repeatedValue:String()))
}
multArray[i][Int(arc4random_uniform(UInt8(multArray[i].count)))]
multArray[i][Int(arc4random_uniform(multArray[i].count))]
multArray[i][Int(random(multArray[i].count))]
multArray[i][random(multArray[i].count)]
multArray[i][Int(arc4random_uniform(0))]
Answer the question
In order to leave comments, you need to log in
// Playground - noun: a place where people can play
import UIKit
var multArray:
multArray = Array(count:10, repeatedValue:Array(count:10, repeatedValue:"What"))
// Got 2- x dimensional array "matrix" 10x10
for i in 0..<10 {
let j = multArray[i].count
println(multArray[i][Int(arc4random_uniform(UInt32(j-1)))])
}
Try this.
multArray[i][Int(arc4random_uniform(0))%multArray[i].count]
So?
UInt32 count = multArray[i].count;
multArray[i][Int(arc4random_uniform(count))]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question