Search code examples
swiftuitableviewcompiler-errorspicker

Swift Issue with adding value to array for use within UITableView Controller


Confused as to why getting error "Cannot subscript a value of type 'inout [[String]]' (aka 'inout Array>'). Within a working table view class

(Originally followed Jared Davidson tutorial https://www.youtube.com/watch?v=pR6dR-vVZeY)

var secondArray = [SecondTable]()

let latest = ViewController().getArrayListLast()
var latestClass = latest.getFullClasses()

print(latestClass[0])

for i in 0...(latest.getAssetClasses().count)
{
    if SecondTable(secondTitle: latestClass[i]) != nil
    {
       secondArray = secondArray.append(SecondTable(secondTitle: latestClass[i]))
    }
}

Solution

  • append(_:) is mutating function and it doesn't return anything.

    Now your code making 3 more mistakes.

    1. As @vadian mentioned in comment ViewController() will never work when using storyboard.

    2. It will give you run time crash index out of bounds because you have provide for loop till the array count but it should be till the less one of the array count, so it should like for i in 0..<(latest.getAssetClasses().count)

    3. You are creating two times object with init of SecondTable to just check for the nil instead of that you can use if let.

      if let secondTable = SecondTable(secondTitle: latestClass[i]) {
         secondArray.append(secondTable)
      }