0

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

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Sebastian Hubard
  • 163
  • 1
  • 4
  • 18
  • 1
    @matt Lazy for structs is not the best idea... I would recommend to keep the initial value as `[]` and in `restart()` set it as `[aList, bList, cList, dList].shuffled()`. Probably a duplicate of another question but I cannot find the correct one. – Sulthan Apr 11 '20 at 17:27
  • @matt I'm sorry but I'm still fairly novice and I'm not sure how that similar question you linked answers my question. Making it lazy still makes the following error for the final return statement: "Cannot convert return expression of type '[String]' to return type 'String'" – Sebastian Hubard Apr 11 '20 at 17:49
  • @Sulthan when you say "in ```restart()```", where do I place the ```[aList, bList, cList, dList].shuffled()```. I'm assuming in the mutating func where I call restart but I'm not sure how to re-write this statement properly – Sebastian Hubard Apr 11 '20 at 17:53
  • `var enabledArrays: [String] = []` and in `restart`: `enabledArrays = [aList, bList, cList, dList].shuffled()`. Is that enough? :) – Sulthan Apr 11 '20 at 18:12

0 Answers0