Christoph Drescher
Christoph Drescher

Reputation: 67

Left side of mutating operator isn't mutable: 'visiter' is a 'let' constant? WHY?

at the point where I tried to subtract the costs from the money of the guest, the error ''Left side of mutating operator isn't mutable: 'visiter' is a 'let' constant'' was throw when I tried to use the operator -= But why? I declared it as variable! What can I do? The error was thrown at the entry function and the bar function:

struct ClubGuest: Guest {
    var name: String
    var money: Double
    var onDrugs: Bool
}
var mikel = ClubGuest(name: "Mikel", money: 50.5, onDrugs: false)
var angelina = ClubGuest(name: "Angelina", money: 400, onDrugs: true)
var steve = ClubGuest(name: "Steve", money: 100.80, onDrugs: true)
var july = ClubGuest(name: "July", money: 1050, onDrugs: false)

struct Club: Party {
    var name: String
    var location: String
    var entryPrice: Double
    var guests: [Guest]
    var maximumguests: Int
    var revenue = 0.0
    mutating func entry(visiter: Guest) {
        if visiter.money > entryPrice && guests.count < maximumguests {
            guests.append(visiter)
            visiter.money -= entryPrice
            revenue += entryPrice
        } else {
            stop()
        }
        GuestCheckUP(guest: visiter)
    }
    
    mutating func Bar(guest: Guest, drink: Drink) {
        print("\(guest) want to buy a drink")
        if guest.money >= drink.price {
            guest.money -= drink.price
        revenue += drink.price
        print("\(guest.name) bought a \(drink.name) for \(drink.price)$, now he/she has \(guest.money)$ in his/her pocket")
        } else {
            print("\(guest.name) has too less money for a drink!")
        }
    }
    
    
    mutating func GuestCheckUP(guest: Guest) {
        if guest.onDrugs {
            throwOut(visiter: guest)
            
        }
    }
    
    
    mutating func throwOut(visiter: Guest) {
        print("\(visiter.name) was throwed out by the security!")
    }
    
    func stop() {
        print("Stop! No entry!")
    }
    func Earnings() {
        print("The earnings of tonight are \(revenue)$")
    }
}

Upvotes: 1

Views: 744

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236458

Note that Guest is a protocol, not a type. You should first make sure in your Guest protocol to declare money property to require a setter, make your method generic and add inout keyword to it:

protocol Guest {
    var money: Double { get set }
    // your code
}

mutating func entry<T: Guest>(visiter: inout T) {
    // your code
}

Upvotes: 3

Related Questions