I am passing a structure into a method. Everything is defined as var, yet within the method where I am just changing one of it's properties,
I get the following error :
Code Block BackGroundStruct.h = 0.1
I get the following error :
I purposely set everything to var and am unsure as to what is going wrong here. Also please excuse the sloppy variable naming here.Cannot assign to property: 'BackGroundStruct' is a 'let' constant
Code Block /* structure */ struct backGroundStruct { var w : CGFloat var h : CGFloat var bGSprite : SKSpriteNode } /*method*/ func heresJohnny (BackGroundStruct : backGroundStruct) { BackGroundStruct.h = 0.1 } /* declare backGroundstruct */ var wnh : backGroundStruct /* create background and pass it to struct init*/ var background = SKSpriteNode(imageNamed: "background") background.size = self.size wnh = backGroundStruct.init(w: background.size.width, h: background.size.height, bGSprite: background) /* call method */ pegBaseBoard.heresJohnny(BackGroundStruct : wnh)
hi,
when you pass a struct as an argument, you get a read-only copy of the struct as the argument, as if it were a let constant. so your (slimmed-down, let's avoid the SpriteNode stuff) code is basically this:
and this is essentially what your function call is doing (which generates the same error trying to modify the argument):
the wnh variable would not be changed in the code above.
if you really want your function to work the way you thought it should -- modifying what is passed in -- you'd have to make it an inout argument and pass it as such:
the wnh variable has been changed in the code above.
one suggestion: by convention, Swift code usually is written with struct type names in UpperCamelCase and variables in lowerCamelCase. your backGroundStruct and BackGroundStruct name choices run somewhat counter to that convention.
hope that helps,
DMG
when you pass a struct as an argument, you get a read-only copy of the struct as the argument, as if it were a let constant. so your (slimmed-down, let's avoid the SpriteNode stuff) code is basically this:
Code Block struct backGroundStruct { var w : CGFloat var h : CGFloat } func heresJohnny (BackGroundStruct : backGroundStruct) { BackGroundStruct.h = 0.1 } var wnh : backGroundStruct wnh = backGroundStruct.init(w: 10, h: 10) heresJohnny(BackGroundStruct : wnh)
and this is essentially what your function call is doing (which generates the same error trying to modify the argument):
Code Block var wnh : backGroundStruct wnh = backGroundStruct.init(w: 10, h: 10) let argument = wnh // a read-only copy to be passed argument.h = 0.1 // what the function is doing in heresJohnny(BackGroundStruct : wnh)
the wnh variable would not be changed in the code above.
if you really want your function to work the way you thought it should -- modifying what is passed in -- you'd have to make it an inout argument and pass it as such:
Code Block func heresJohnny (BackGroundStruct : inout backGroundStruct) { BackGroundStruct.h = 0.1 } var wnh : backGroundStruct wnh = backGroundStruct.init(w: 10, h: 10) heresJohnny(BackGroundStruct : &wnh)
the wnh variable has been changed in the code above.
one suggestion: by convention, Swift code usually is written with struct type names in UpperCamelCase and variables in lowerCamelCase. your backGroundStruct and BackGroundStruct name choices run somewhat counter to that convention.
hope that helps,
DMG