I need to pass the tuple arrays as parameters for a function that selects the random tuple from the arrays considering also two other parameters) This function returns the index, that I will use then to play this random file. And this function is in the class fileManager()
class fileManager {
var drums = [
("drum", "drum", 8, 2, false, 153, "C", 1),
("drum", "drum2", 6, 3, false, 153, "C", 1),
("drum", "drum3", 8, 2, false, 153, "C", 1),
("drum", "drum4", 4, 2, false, 153, "C", 1),
("drum", "drum5", 8, 1, false, 153, "C", 1) ]
var piano = [
("piano", "piano", 8, 2, false, 153, "C", 1),
("piano", "piano2", 8, 3, false, 153, "C", 1),
("piano", "piano3", 8, 1, false, 153, "C", 1)
]
//instrumentArray will be any instrument, Strength - we need to find, KeyNote - we need to find in Array.
func randomizeTheNextInstrument (instrumentArrayInput: [(String,String,Int,Int,Bool,Int,String, Int)], Strength: Int , KeyNote: String) -> Int {
var instrumentArray = instrumentArrayInput
var indexInstrument: Int!
for index in 0...instrumentArray.count {
if instrumentArray[index].4 == true { //check if played
instrumentArray.removeAtIndex(index)
}
if instrumentArray[index].6 != KeyNote { // check keyNote
instrumentArray.removeAtIndex(index)
}
if instrumentArray[index].3 != Strength { // check strength
instrumentArray.removeAtIndex(index)
}
}
var indexToChoose: Int = Int(arc4random_uniform(UInt32(instrumentArray.count)))
for index in 0...instrumentArrayInput.count {
if instrumentArrayInput[index].1 == instrumentArray[indexToChoose].1 { // finds what one in ArrayInpu equals the randomized one in Array that chosen
indexInstrument = index
}
}
return indexInstrument
}
}
But when I call the function from another class by doing this.
indexToPlay = Int(fileManager().randomizeTheNextInstrument(fileManager().drums, Strength: drumStrength, KeyNote: "C"))
It gives me fatal error: "Array index out of range (lldb)" It writes me that the array instrumentArrayInput has 5 values, but values are 'none' and instrumentArray has 3 values, but values are 'none'. The next thing, which looks strange is indexToChoose = 140734791422096. And it also writes me EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, sub code=0x0) for the string with:
if instrumentArray[index].4 == true {
What's I doing wrong?