Cannot Use Mutating Member on Immutable Value of Type

Cannot use mutating member on immutable value of type

When you apply type casting to value types (such structs), if succeed, you receive immutable copy of requested value:

(self.data as! ModelOne) // this is copy of data

The only way (as known to me) how you can mutate values that need to be casted - reassign value (as @Sahil Beri pointed you need declare variable):

func someFunc() {
if var data = data as? ModelOne {
data.setSub(ModelOne.SubModelOne(someVar: 2, otherVar: 1))
self.data = data // you can do this since ModelOne conforms to SuperModel
}
}

Cannot use mutating member on immutable value: 'parent' is a 'let' constant?

As you are passing a struct (copies by value), you can't mutate function parameters without an inout.

Edit your code to this and try again:

static func createType(name: String, parent: inout FianceItem?) -> FianceItem {

You will also have to do this change if you want to pass nils:

var nilParent: FianceItem? = nil
var root = FianceItem.createType(name: "MyRoot", parent:&nilParent)

Cannot use mutating member on immutable value: Error in Swift


var quarantineStuff: [String] = ["zero to one book", "proper twelve whiskey", 
"coffee", "tonic water", "toilet roll", "broken electronics"]

func removeItemsICantTakeWithMe(list: [String]) -> [String] {
var aList = list // method object is immutable so we have to make a mutable object

if let index = aList.firstIndex(of: "toilet roll") {
aList.remove(at: index)
}
return aList
}

let renewedList = removeItemsICantTakeWithMe(list: quarantineStuff)
print(renewedList)

Cannot modify my array - Cannot use mutating member on immutable value: function call returns immutable value

As the error tells you, your findPerson method is returning an immutable struct, so you can't edit it.

There are a few solutions to this -- two are included in the code sample below:

final class City {
var people = [Person]()

func addPetFor(id: UUID, newPets: [String]) {
guard let index = self.people.firstIndex(where: { $0.id == id }) else {
assertionFailure("Not found")
return
}
self.people[index].pets.append(contentsOf: newPets)
}

func addPetFor2(id: UUID, newPets: [String]) {
self.people = self.people.map {
if $0.id == id {
var personCopy = $0
personCopy.pets.append(contentsOf: newPets)
return personCopy
} else {
return $0
}
}
}
}

In both cases, the code has a reference to where the Person belongs in the people array, so it can modify it. Note that people also has to be var, not let, for the same reason -- it needs to be mutable.

Swift(UI) Error: Cannot use mutating member on immutable value: 'self' is immutable

Views are immutable in SwiftUI. You can only mutate their state, which is done by changing the properties that have a @State property wrapper:

@State var entries: [CEntries] = []

However, while you could do that, in your case CEntries is a class - i.e. a reference type - so while you could detect changes in the array of entries - additions and removals of elements, you won't be able to detect changes in the elements themselves, for example when .string1 property is updated.

And it doesn't help that it's an ObservableObject.

Instead, change CEntries to be a struct - a value type, so that if it changes, the value itself will change:

struct CEntries: Identifiable {
var id: UUID = .init()
var string1 = ""
var string2 = ""
}

struct AView: View {

@State var entries = [CEntries]()

var body: some View {
VStack() {
ForEach(entries) { entry in
VStack {
Text(entry.string1)
Text(entry.string2)
}
}
Button(action: {
self.entries.append(CEntries(string1: "he", string2: "lp"))
}) {
someButtonStyle()
}
}
}
}

swift: Cannot use mutating member on immutable value: subscript is get-only

You need to extend MutableCollection. A normal Collection does not support setting through subscripts, the mutable counterpart does.

extension MutableCollection where Index == Int, Element == [MyData] {
//...
}

cannot use mutating member on immutable value: 'XXX' is a 'let' constant

The error-message already tells you, what you need to change. Replace let with var.


structs ar value types. This means, if any property of a struct is modified, the instance needs to be declared as var.

When your struct has a method, that is modifying one of the struct's properties, this method is mutable. mutable methods can only be called on instances you have declared as var.

Mutating an array within a view from a subview Cannot use mutating member on immutable value: 'self' is immutable

SwiftUI view is struct you cannot mutate it from within self. Make notes as @State then you can use

    @State var notes: [String] = []

// .. other code

func addNote(note: String) {
notes.append(note)
}


Related Topics



Leave a reply



Submit