-2

I have return one function:

func abc() -> [Int] {
    return [1,2,3,4]
}

I'm calling this function and trying to modify the return value

abc().sort()

It gives this error:
"Cannot use mutating member on immutable value: 'abc' returns immutable value".

I know in order to change the value inside the function we need to use inout parameter but how to change the value which is returned from the function in swift?

0-1
  • 702
  • 1
  • 10
  • 30
  • Does this answer your question? [Cannot use mutating member on immutable value: function call returns immutable value - not sure why value is immutable](https://stackoverflow.com/questions/48287795/cannot-use-mutating-member-on-immutable-value-function-call-returns-immutable-v) (Specifically: Instead of using a mutating functions `shuffle()`, you can use a variant that returns a shuffled copy `shuffled()`, similar to `sort()` vs `sorted()`) – 0-1 Jul 08 '20 at 17:02

1 Answers1

2

You can only assign the result to a new variable with sorted()

let sortedArray = abc().sorted()
vadian
  • 274,689
  • 30
  • 353
  • 361