0

sorry to bother with something that is probably pretty easy.

Here is my code now the only difference is instead of 'struct' it was a 'class'. Now it's causing the changeAmount function to no longer work. I have tried declaring it as 'let' etc, but it shows other errors. If anyone has a fix that would be great.

struct Product {
    var ID : String = ""
    var name : String = ""
    var type : String = ""
    var price : Double = 0.00
    var image : String = ""
    var amount : Int = 1
    var imageURL : String = ""
    var thumbnailURL : String = ""
    var description : String = ""

    init(id:String, name:String, description:String, type:String, price:Double, image:String, imageURL:String, thumbnailURL:String, amount:Int = 1) {
        self.ID = id
        self.name = name
        self.description = description
        self.type = type
        self.price = price
        self.image = image
        self.imageURL = imageURL
        self.thumbnailURL = thumbnailURL
        self.amount = amount
    }

    func changeAmount(a:Int){ self.amount = a }
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
Austin Sweat
  • 101
  • 11

1 Answers1

1

If a function will change the contents of a struct, you need to declare it as mutating:

mutating func changeAmount(a:Int){
    self.amount = a
}
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287