0

I cannot seem to figure this simple error out. Any help would be appreciated.

Here is the line of code which has an issue, as well as the error message I receive:

fun.dataSource = TestArrayData[result] <------Cannot subscript a value of type '[TestDataModel]' with an argument of type 'TestDataModel'

I'm also getting this error message for the same line: 1. Overloads for 'subscript' exist with these partially matching parameter lists: ((UnboundedRange_) -> ()), (Int), (Range<Int>), (Range<Self.Index>)

Here is the full brick of code:

func fun(at index: Int) -> FunView {
    let fun = FunView()

    let myArray = [generateRandomNumber(0, 5, 1)]
    var myString = ""
    _ = myArray.map{ myString = myString + "\($0)" }
    let ayyy = (myString as NSString).integerValue
    let myInt = Int?(ayyy)
    var ayyString = String(ayyy)

    let result = (TestDataModel(text: ayyString))
    fun.dataSource = TestArrayData[result]
        return fun
}

Following up for @Paulw11 , the TestArrayData is the following:

var TestArrayData = [TestDataModel(text: "One"),
                     TestDataModel(text: "Two"),
                     TestDataModel(text: "Three"),
                     TestDataModel(text: "Four"),
                     TestDataModel(text: "Five"),
                     TestDataModel(text: "Six")]
CodeMan123
  • 35
  • 7
  • As the error says, you can't index an array using a variable of type `TestDataModel`, which is what `result` is. You need an `Int` index. Did you mean to initialise an instance of `TestArrayData` and used the wrong brackets? I.e. did you want `TestArrayData(result)`? If not, what is `TestArrayData`? What is the type of `fun.dataSource`? – Paulw11 Nov 17 '19 at 19:01
  • @Paulw11 I replied above. Thank you! – CodeMan123 Nov 17 '19 at 19:07
  • Ok, so it is just an array (it should start with a lower case letter, since it isn't a class). You need to subscript the array with an integer. Perhaps the unused `index` parameter? Can you explain what you are trying to do? – Paulw11 Nov 17 '19 at 19:09
  • Gotcha, I will make that lowercase. Okay! Where should I input the subscript in the code, and what should the subscript function look like? This is what I have for the subscript, but I think it's incorrect: `subscript(bounds: (UnsafePointer?, Int32) -> UnsafeMutablePointer?) -> TestDataModel { get { return text[index] } set { text[index] = result } } }` Also, what I'm trying to do: I'll reply again. – CodeMan123 Nov 17 '19 at 19:13
  • It isn't clear to me what this code is supposed to achieve. You want a value assigned to `fun.dataSource` but what value? – Paulw11 Nov 17 '19 at 19:15
  • I'm trying to get a one of the dict/array values pulled at random, and to never return a duplicate. So: If I were to "print" the end result, one of the possible combinations would be `Two`, `One`, `Six`, `Five`, `Three`, `Four` this would essentially pull all of the data in the array/dict, until the list is exhausted and done. – CodeMan123 Nov 17 '19 at 19:15
  • I want a String/Label assigned to fun.datasource @Paulw11 – CodeMan123 Nov 17 '19 at 19:17
  • Ok, so take a look at [this](https://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift). You can just `shuffle` the array and then iterate over it. – Paulw11 Nov 17 '19 at 19:19
  • Does this work for `Dict`'s? @Paulw11 – CodeMan123 Nov 17 '19 at 19:20
  • Btw, I don't want them all returned at once like the `shuffle()` func does, I want them individually pulled with each time I compile the code. @Paulw11 – CodeMan123 Nov 17 '19 at 19:22
  • I understand that. You would need to take your initial array, have a second property that holds `testArrayData.shuffled()` and create a property for the current `index`. Dictionaries are unordered so you can't shuffle them directly. You could store the keys in an array and shuffle the array of keys. Then use the keys in that shuffled array to access the dictionary. – Paulw11 Nov 17 '19 at 19:34

0 Answers0