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

Lorenzo Santini

Inside my app I created a Protocol called "Positions" and extended the UIView class to conform to this protocol in order to add some properties to the UIView class. I used the code below.

import Foundation
import UIKit

protocol Positions {
    var initialPositions: [CGRect] {get set}
    var finalPositions: [CGRect] {get set}
    var positionsAreDefined: Bool {get}
}

public enum PositionsType {
    case initial
}

private var initialPositionKey: UInt = 0
private var finalPositionKey: UInt = 0

extension Positions {
    var initialPositions: [CGRect] {
        get {
            if objc_getAssociatedObject(self, &initialPositionKey) != nil {
                return objc_getAssociatedObject(self, &initialPositionKey) as! [CGRect]
            } else {
                return [] as! [CGRect]
            }

        }
        set {
            objc_setAssociatedObject(self, &initialPositionKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    var finalPositions: [CGRect] {
        get {
            if objc_getAssociatedObject(self, &finalPositionKey) != nil {
                return objc_getAssociatedObject(self, &finalPositionKey) as! [CGRect]
            } else {
                return [] as! [CGRect]
            }
        }

        set {
            objc_setAssociatedObject(self, &finalPositionKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    var positionsAreDefined: Bool {
        get {return initialPositions.count != 0 && finalPositions.count != 0 && initialPositions.count == finalPositions.count}
    }

    var positionsCount: Int {
        get {return initialPositions.count}
    }
}

extension UIView: Positions {}

Well, today I tried to extend the UIView class with a new method that changes the added-by-me properties but the compiler gave me this error on the line where I tried to modify the properties value:

Cannot assign to property: 'self' is immutable

Here's the method:

public func horizontallyInContainer(withShift shift: CGFloat, forExpansionNumber index: Int, forPositionType type: PositionsType) {
    if self.positionsAreDefined && self.superview != nil {
        if type == .initial {

            var newPositions = self.initialPositions
            newPositions[index].origin.x = self.superview!.bounds.width * 0.5 - self.bounds.width + shift
            self.initialPositions = newPositions //Here happens the error

        } else {

            var newPositions = self.finalPositions
            newPositions[index].origin.x = self.superview!.bounds.width * 0.5 - self.bounds.width + shift
            self.finalPositions = newPositions //Also here happens

        }
    }
}

Can anyone explain me the nature of that error?

vadian

Make the protocol become reference type

protocol Positions: AnyObject {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

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

Cannot assign to property: 'self' is immutable

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

"Cannot assign to property: "any" is immutable" error

Swift error: Cannot assign to immutable value

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

Cannot assign to property: '$0' is immutable

Cannot assign to property: '$text' is immutable

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

Swift error in SwiftUI Button action closure: "Cannot use mutating member on immutable value: 'self' is immutable"

Swift 2 Error using mutating function in Protocol extension "Cannot use mutating member on immutable value: 'self' is immutable

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

Cannot assign to property: "description" property is immutable error with ( leftSwipe.description = .Left)

Swift Cannot assign to self in a method

Cannot assign value, Swift treat is as immutable

Swift Cannot Assign to immutable value of type NSData

Swift: Cannot assign to immutable expression of type 'AnyObject?!'

Cannot assign to immutable expression of type Swift

error: cannot assign to immutable indexed content `i[..]`

Swift error saying it cannot assign to property due to it being a 'let' constant

Cannot use mutating getter on immutable value: 'self' is immutable error

Cannot assign to property: function call returns immutable value

Cannot assign to "" in "self" - Working with Tuples in Swift

How to fix Swift 3 error ‘cannot use mutating member on immutable value: ‘loadedPost’ get only property’?

Swift cannot assign immutable value of type [CLLocationCoordinate2D]

Cannot assign this CGFloat to a property in XCode using Swift

Error - Cannot assign to value: function call returns immutable value

swift4: "cannot use mutating member on immutable value: 'self' is immutable" in mutating function

Cannot create property label on string error on immutable objects probably