swift - binary operator += cannot be applied to two Int operands -


i'm creating simple struct.

struct expenses {      var totalexpenses:int = 0      func addexpense(expense: int) {         totalexpenses += expense     } } 

it produce error @ beginning of line totalexpenses += expense error message is

binary operator += cannot applied 2 int operands.

why getting error message , how can solve issue?

you need specify addexpense mutating function, so:

struct expenses {     var totalexpenses:int = 0      mutating func addexpense(expense: int) {         totalexpenses += expense     } } 

from documentation:

structures , enumerations value types. default, properties of value type cannot modified within instance methods.

however, if need modify properties of structure or enumeration within particular method, can opt in mutating behavior method.

for more information see the swift programming language: methods


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -