0

Error:

Type mismatch: inferred type is Array<MutableSet<String>?> but IntArray was expected

Code:

  fun tab10(): IntArray {
        var Arr1 = arrayOf(player.chart_keys)
        return Arr1
  }

chart_keys is in a different class Player

var chart_keys: MutableSet<String>? = mutableSetOf(),

Is it possible?

7
  • what is IntArray Show which code produces this error Commented Sep 27, 2019 at 10:15
  • @johnrao07 what is player.chart_keys Commented Sep 27, 2019 at 10:26
  • arrayOf will create an array of whatever you pass into it. Therefore, passing a MutableSet<String>into it will create array of MutableSet<String> Commented Sep 27, 2019 at 10:34
  • 2
    kotlin can't predict how exactly you want to convert set of Strings to array of Integers.Therefore you have to tell it explicitly (by implementing manually) Commented Sep 27, 2019 at 10:50
  • 1
    What is chart_keys? Why does it have a _ instead of K? Why is the set mutable? Can it be immutable? Commented Sep 27, 2019 at 13:16

1 Answer 1

3

You need to map Strings to Ints and then convert the Set to an IntArray. This might work:

player.chart_keys?.map { it.toInt() }?.toIntArray()
   ?: intArrayOf()
Sign up to request clarification or add additional context in comments.

3 Comments

I think it would be player.chart_keys?.map { it.toInt() }.toIntArray(), and I'm not sure if ones better than the other but instead of intArrayOf() I would use emptyArray()
Good catch! You'll notice in my edit that I only spotted it was nullable after writing that line! Fixing now :) Are sure emptyArray() can create an IntArray?
ah, you're right - emptyArray() makes a Array<Int> - just ignore the second half of my comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.