Hello, how could I convert the bellow Swift code t...
# android
b
Hello, how could I convert the bellow Swift code to Kotlin? I'm coming from iOS and I use a Redux implementation for state management ("state")
Copy code
state.list = state.list.map { listItem in
        if listItem.id == action.payload.id {
            var listItem = listItem
            listItem.title = action.payload.newTitle
            return listItem
          } else {
            return listItem
          }
        }
k
Replace the in with ->
b
@kenkyee I tried with -> but I receive error "Type Mismatch Required: ArrayList<>, found List<Unit>"
k
How did you define state.list? Should not be arraylist
b
What should be? MutableList is better?
m
The lambda should not use returns. That is making the
map
function see the lambda as returning
Unit
. As for the type of
state.list
. Ideally that would be of type
List
. If it needs to be mutable, then it should be
MutableList
and you will need to call
toMutableList
on the result of
map
.
☝️ 1
b
@mkrussel I tried with List and removed "return" but no luck. Could you recreate my function?
m
Copy code
state.list = state.list.map { listItem in
        if listItem.id == action.payload.id {
            var listItem = listItem
            listItem.title = action.payload.newTitle
            listItem
          } else {
            listItem
          }
        }
Although this is not really doing any mapping. This is probably better to be in a basic for loop.