This is my func:
func adjustPointOffset(
point: PointType,
isBefore: Bool,
key: Key,
target: Node,
textLength: Int) {
if point.type == "text" {
point.key = key// error: Cannot assign to property: 'point' is a 'let' constant
if !isBefore {
point.offset += textLength// error: Left side of mutating operator isn't mutable: 'point' is a 'let' constant
}
} else if point.offset > target.getIndexWithinParent() {
point.offset -= 1// error: Left side of mutating operator isn't mutable: 'point' is a 'let' constant
}
}
This is PointType class:
enum SelectionType: String {
case text = "text"
case element = "element"
}
protocol PointType {
var key: NodeKey { get set }
var offset: Int { get set }
var type: SelectionType { get set }
func isPointType(point: PointType) -> Bool
func isBefore(point: PointType) -> Bool
func getNode() -> Node
func setPointType(key: NodeKey, offset: Int, type: SelectionType)
func getCharacterOffset() -> Int
func isAtNodeEnd() -> Bool
}
class Point: PointType {
var key: Key
var offset: Int
var type: SelectionType
init(key: Key, offset: Int, type: SelectionType) {
self.key = key
self.offset = offset
self.type = type
}
}
extension Point: Equatable {
static func == (lhs: Point, rhs: Point) -> Bool {
return lhs.key == rhs.key &&
lhs.offset == rhs.offset &&
lhs.type == rhs.type
}
}
How do I solve these errors – error: Cannot assign to property: ‘point’ is a ‘let’ constant. I don’t understand ‘point’ is a protocol and the values are all ‘var’. But its pointing to ‘let’.
>Solution :
PointType as a protocol, as written, could apply to either a value type (such as a struct) or a reference type (such as a class). If it were a value type and passed to your function without inout, it would be immutable.
If your PointType is always going to be a reference type (and thus always mutable from inside a function where it’s pointed to by reference), you can fix your error by saying that it has to be AnyObject:
protocol PointType : AnyObject {