-2

I'm trying to switch out a direct integer with a variable in swift, but for some reason I'm getting this error and I have no idea. The end goal is to get my currentValue (line 76) to replace the 100's on line 41 - could anyone let me know how I could accomplish this without the error? New to swift and having a hard time (background in objective-c, figured something this simple would not stop me in my tracks!)

Full .swift file here: http://pastebin.com/K6UHkNEv

EDIT:

// these values change the number of squares
let _gameView = CGOLView(gridWidth:100, gridHeight:100)

@IBOutlet weak var tileSizeSlider: UISlider!
@IBAction func sliderValueChanged(sender: UISlider) {
    var currentValue = Int(sender.value)
    print("\(currentValue)")
}

should work as:

// these values change the number of squares
let _gameView = CGOLView(gridWidth:currentValue, gridHeight:currentValue)

@IBOutlet weak var tileSizeSlider: UISlider!
@IBAction func sliderValueChanged(sender: UISlider) {
    var currentValue = Int(sender.value)
    print("\(currentValue)")
}

instead I get this error:

Use of unresolved identifier 'currentValue'

and if I try to create custom int's and input them:

   var gridWidthValue = 50
   var gridHeightValue = 50

like this:

let _gameView = CGOLView(gridWidth:gridWidthValue, gridHeight:gridHeightValue)

I get:

'ViewController.Type' does not have a member named 'gridHeightValue'

Any help would be appreciated - thanks stackoverflow community!

David.

  • Add the code to the question via copy/paste, not in a link. Just the lines that cause the error and the complete error message. – zaph Nov 30 '14 at 01:08
  • @Zaph Can't believe I actually forgot to add the actual error and code - I apologize! Edited. – davidvanbeveren Nov 30 '14 at 01:30

2 Answers2

0

Your problem is that you cannot access the variable currentValue because it is inside of a function. You have to declare that value outside of the function to be able to use it outside of the function.

Wiem
  • 163
  • 8
  • I can probably fix that, but the problem I'm worried about is that when I created custom variables (see second part to my problem) that other error showed up, which leads me to believe it won't accept a variable anyways. – davidvanbeveren Nov 30 '14 at 02:00
0

currentValue is a local variable to sliderValueChanged.

Instead you should instantiate _gameView in init. Note however, you still won't be able to use currentValue.

If this is a one off sort of thing, you can always make _gameView an optional and then create it when you have adjusted the slider. This is admittedly a little clumsy.

I am not familiar with Conway's Game of Life, but looking at the code, it seems CGOLView's init does some adjustment based on the grid width and height. The reason I am mentioning this is that you could always change the view's frame size, however, you'd then also need to make some other mods to the tileViews for it to look proper.

As to why gridWidthValue/gridHeightValue is not working. Those are properties defined in an instance. Hence you would need to do somethign like self.gridWithValue to reference it. However, you cannot do that when defining the property such as

let _gameView = CGOLView(gridWidth:gridWidthValue, gridHeight:gridHeightValue)

This is also why instantiating _gameView in init is the way to go.

Mobile Ben
  • 7,121
  • 1
  • 27
  • 43
  • Thanks. I moved my custom variables outside of the class and they worked, so this leads me to believe that if I can access currentValue which is stuck in the method, I will be able to do what I need to do. If possible, could you recommend how I might be able to access currentValue's value from outside of the brackets? Cheers, and thanks for the time and explanation! – davidvanbeveren Nov 30 '14 at 02:20
  • You can create a property with a default initial value doing something like var currentValue:Int = 50. Although it would be better to name it something like gridValue so it is more clear what it is. Also, this means that you need to decide how you adjust the view frame size. You can instantiate a new one, but that means removing/adding the view and could have side effects depending on the other views. Or you can add an extension onto CGOLView to change the frame size which will handle the resize properly. The latter is probably the way to go. – Mobile Ben Nov 30 '14 at 02:32
  • Rock on @Mobile! I decided to do that, and it works great - global property that can be accessed from anywhere grabs currentValue and inputs it into the area where it accepts the size ints. Now, the view isnt updating when the scroll is used, obviously I need the view to "refresh" or "update" when the value is changed (like you suggested). I'm really too new to be able to code anything by myself - is there any way you could give me more of an explanation on the extension idea? – davidvanbeveren Nov 30 '14 at 02:50