Cannot Assign to Value: 'self' Is Immutable

Cannot assign to property: 'self' is immutable, I know how to fix but needs understanding

struct is a value type. For value types, only methods explicitly marked as mutating can modify the properties of self, so this is not possible within a computed property.

If you change struct to be a class then your code compiles without problems.

Structs are value types which means they are copied when they are passed around.So if you change a copy you are changing only that copy, not the original and not any other copies which might be around.If your struct is immutable then all automatic copies resulting from being passed by value will be the same.If you want to change it you have to consciously do it by creating a new instance of the struct with the modified data. (not a copy)

Swift cannot assign immutable value of type [CLLocationCoordinate2D]

When passing parameters to a function, they are passed as immutable by default. The same as if you declared them as a let.

When you pass the coords param into the MGPolygon method, it's passed as an inout parameter, which means those values can change, but because the parameter is an immutable value by default, the compiler complains.

You can fix it by explicitly telling the compiler that this parameter can be modified by prefixing it with a var.

func drawShape(var coords: [CLLocationCoordinate2D]) {
var shape = MGLPolygon(coordinates: &coords, count: UInt(coords.count))
mapView.addAnnotation(shape)
}

Prefixing a parameter with var means that you can mutate that value within the function.

Edit: Swift 2.2

Use the keyword inout instead.

func drawShape(inout coords: [CLLocationCoordinate2D]) {
var shape = MGLPolygon(coordinates: &coords, count: UInt(coords.count))
mapView.addAnnotation(shape)
}

Unexpected Cannot assign to property 'self' is immutable compile time error IN CLASS

It's a bug, but not the bug you might think (and the error message is no help at all). It's a known bug caused by the fact that the keyboardAppearance property of the UITextInputTraits protocol is an optional property (meaning that it might not be implemented). This is an Objective C feature, not a Swift feature, and the bug is that such properties are not directly settable in Swift, even when they are marked {get set}.

To see this, let's emulate the same schema for ourselves:

@objc public protocol P {
@objc optional var keyboardAppearance : NSString {get set}
}

open class C : NSObject {
private var wrapped: P
public init(wrapped: P) {
self.wrapped = wrapped
super.init()
}
open var keyboardAppearance : NSString {
get {
wrapped.keyboardAppearance ?? "" as NSString
}
set {
wrapped.keyboardAppearance = newValue
}
}
}

We get the same error. But if you delete the keyword optional, the error goes away. This proves that the optional is what causes the issue. But you cannot delete optional in your situation, because that protocol doesn't belong to you.

The workaround is the same as for the original bug — use a key path:

let kp = \C.wrapped.keyboardAppearance
self[keyPath:kp] = newValue

Cannot assign to property: 'self' is immutable swift error

Make the protocol become reference type

protocol Positions: AnyObject {

Swiftui Cannot assign to property: 'self' is immutable Error

Define your currentMood as a @State variable:

@State var currentMood: String

You'll likely need to assign it with self.currentMood = moodData (adding self.)

Cannot assign to property: 'self' is immutable while assigning value to @ObservedObject

Even if you could mutate self, you don't want to do this.

An @ObservedObject is a reference to an object instance that is injected into your view, typically from a superview, or if ParentView is your root view, from your scene delegate.

By assigning a new value to selectedFruit you would be changing the object referred to in this view. but the parent view would still refer to the original object.

Your selectedFruit should be an @Published property of your viewModel. You would then have self.viewModel.selectedFruit = record in your onTapGesture.

This Paul Hudson article provides a good overview of the difference between an observable object, a state property and the environment.



Related Topics



Leave a reply



Submit