0

I have a var

var soketTasksList:Set<SocketTask> {
   get { return socketManager.tasksList }
}

I don't need to set, only get, but I need do something like this

soketTasksList.remove(task)

but compiler says

Cannot use mutating member on immutable value is a get-only property

I tried to add the keyword 'mutating' to the get, but this isn't working. I also tried to add 'mutating' to the var, but this isn't working either.

UPD i dont undestand why do I need set? if i do

func getSoketTasksList() -> Set<CXSocketTask> {
    return socketManager.tasksList
}

i can

getSoketTasksList().remove(task)

why not with var?

EvGeniy Ilyin
  • 1,817
  • 1
  • 21
  • 38
  • but then it will not be read-only, i don't need do set, i need mutating result from my var – EvGeniy Ilyin Apr 27 '16 at 14:35
  • Is your tasksList declared to be mutable? – gagarwal Apr 27 '16 at 14:43
  • A read-only computed property cannot be mutated without a setter method. Even though you don't need to mutate the the variable holding the list, it will inadvertently make the list immutable. – Jony T Apr 27 '16 at 14:44
  • "i can `getSoketTasksList().remove(task)`" No, you can't! You keep digging yourself deeper into a world of delusion. Try to listen: Set is not NSMutableSet. Set is a Swift value type. NSMutableSet is an Objective-C reference type. If you want to pass a mutable reference around, use NSMutableSet. If you're going to use Swift types, learn how Swift types work. – matt Apr 27 '16 at 18:36

3 Answers3

4

i don't need to set, only get

Yes, you do need to set.

i need do something like this soketTasksList.remove(task)

That is a mutation. Mutating a value type like Set requires the ability to set. But you have cut off that possibility by making this a read-only computed variable.


UPD i dont undestand why do I need set? if i do

   func getSoketTasksList() -> Set<CXSocketTask> {
       return socketManager.tasksList
   }

i can

   getSoketTasksList().remove(task)

No you can't. Try it. Here's a playground test:

class CXSocketTask:NSObject{}
class SocketManager {
    var tasksList = Set<CXSocketTask>()
}
let task = CXSocketTask()
let socketManager = SocketManager()
socketManager.tasksList.insert(task)

func getSoketTasksList() -> Set<CXSocketTask> {
    return socketManager.tasksList
}
getSoketTasksList().remove(task)

The last line generates an error: "cannot use mutating member on immutable value: 'getSoketTasksList' returns immutable value".

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • See for example my answer here: http://stackoverflow.com/a/36000944/341994 As I explain there, a value type is not mutable _in place_. A move like `remove` requires that we be able to _write back to the reference_. But that is exactly what a read-only variable cannot do. – matt Apr 27 '16 at 14:46
  • but i get from var link to object, this is not copy object. and if its link i can do with object anything – EvGeniy Ilyin Apr 27 '16 at 14:47
  • 1
    @EvGeniyIlyin You can _say_ that, but you're wrong. Programming is about facing _reality_, not going with what you _believe_. – matt Apr 27 '16 at 14:56
0

You can use temporary variable for this purpose:

var list = soketTasksList
list.remove(task)

Note that underlying list (socketManager.tasksList in this case) remains untouched.

Alexander Doloz
  • 4,078
  • 1
  • 20
  • 36
  • "Note that underlying list remains untouched" You're absolutely right, but then it _doesn't_ meet the purpose. He _wants_ to mutate the underlying list. And he simply can't do that if his reference to that list is by way of a read-only computed variable. – matt Apr 27 '16 at 17:20
0

The only situation in which you can do this is if the actual mutating is done through something which is settable.

For example:

struct Person {
  var age: Int = 1

  mutating func setAge(a: Int) -> Int {
    age = a
    return a
  }

  var computedAge: Int {
    mutating get {
      setAge(a: 4)
      return age
    }
  }
}

var person = Person()
print(person.computedAge) //Prints 4

This is by design

Cameron Porter
  • 801
  • 6
  • 15