Home > database >  Unresolved reference: indices
Unresolved reference: indices

Time:11-21

everyone. Am new to kotlin. Does anyone have solution to this syntax error am experiencing. the key word "indices in my code keeps flagging an error that says unresolved reference: indices. I have tried update my build.gradle with id 'kotlin-android-extensions' but it seems not to solve the problem.

fun detachCounter(row: Int , col: Int) {
        var index = -1
        for (i in catalog.indices) {
            if (catalog[i].col == col && catalog[i].row == row) {
                index = i
                break
            }
        }
        if (index != -1) {
            catalog.removeAt(index)
            _draughBoard[row][col] = 0
        }
    }

CodePudding user response:

Assuming that catalog is an ArrayList of a class DraughtCounters your code should work. I tested it with a very simple DraughtCounters class:

class DraughtCounters(var row: Int = 0, var col: Int = 0)

val catalog = arrayListOf(
  DraughtCounters(1, 1),
  DraughtCounters(1, 2),
  DraughtCounters(1, 3),
  DraughtCounters(2, 1),
  DraughtCounters(2, 2),
  DraughtCounters(2, 3),
  DraughtCounters(3, 1),
  DraughtCounters(3, 2),
  DraughtCounters(3, 3)
)

... your detachCounter function code here ...

detachCounter(1, 2)
detachCounter(2, 3)
detachCounter(3, 1)

for (item in catalog) {
  println("${item.row}, ${item.col}")
}

If that is indeed your setup with catalog and DraughtCounters then you can further simplify detachCounter to something like this:

fun detachCounter(row: Int, col: Int) {
  val itemToDetach = catalog.firstOrNull { it.row == row && it.col == col }
  if (itemToDetach != null) {
    catalog.remove(itemToDetach)
    // _draughBoard[itemToDetach.row][itemToDetach.col] = 0
  }
}

CodePudding user response:

I have resolved the error by simply updating my build. gradle dependecies

 dependencies {
        classpath 'com.android.tools.build:gradle:7.0.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
  • Related