I'm trying to randomly select a string value from a group of arrays (I'm not even sure if with no bug that the below code would work), but my xcode failed to run the app with the below message:
":0: error: cannot use instance member 'aList' within property initializer; property initializers run before 'self' is available"
I'm not really sure what the issue is since this all worked before I tried converting it from one array to a combination of arrays. Below is my code for it.
import GameKit
import Foundation
struct PromptProvider {
var aListIndex = 0
var bListIndex = 0
var cListIndex = 0
var dListIndex = 0
var enabledArraysIndex = 0
var includeA = true
var includeB = true
var includeC = true
var includeD = true
var aList = [
“A1”,
“A2”,
“A3”,
“A4”]
var bList = [
“B1”,
“B2”,
“B3”,
“B4”]
var cList = [
“C1”,
“C2”,
“C3”,
“C4”]
var dList = [
“D1”,
“D2”,
“D3”,
“D4”]
init() {
self.restart()
}
let enabledArrays = [aList, bList, cList, dList]
mutating func restart() {
self.enabledArrays.shuffle()
self.enabledArraysIndex = 0
}
mutating func randomPrompt() -> String {
if enabledArraysIndex == enabledArrays.count {
return “Out of String Values“
}
else {
defer {enabledArraysIndex += 1}
return enabledArrays[enabledArraysIndex]
}
}
}
Some notes: I used aliases such as "a", "b", "c", to make it easier to understand. I call the restart initializer from the view controller if I want to reset the string values. The main use is in calling promptProvider in the view controller to pull a prompt to a label.
Any help would be greatly appreciated. Thank you