1

This is a super basic question that is troubling me.

I have a UIslider IBAction that is generating a Double (var = rounded). I want to use this double in viewDidLoad. but getting the error "Use of unresolved identifier 'rounded'"

@IBAction func sliderValueChanged(sender: UISlider) {
    var currentValue = Double(sender.value)
    var rounded = Double(round(100*currentValue)/100)
    label.text = "\(rounded)"
}


override func viewDidLoad() {
    super.viewDidLoad()
    let fileURL = NSBundle.mainBundle().URLForResource("puppy", withExtension: "jpg")
    let beginImage = CIImage(contentsOfURL: fileURL)
    let filter = CIFilter(name: "CISepiaTone")
    filter.setValue(beginImage, forKey: kCIInputImageKey)
    filter.setValue(rounded, forKey: kCIInputIntensityKey)
    let newImage = UIImage(CIImage: filter.outputImage)
    self.imageView.image = newImage
}

filter.setValue(rounded, forKey: kCIInputIntensityKey) is where i am getting the error. I want to use the 'rounded' variable from the slider function here.

Any help in using one variable from one function in another function would be very much appreciated. I have ran into this a couple times without success. So once you all help me out here, it should fix my other issues as well.

Thanks

Joe
  • 3,772
  • 3
  • 33
  • 64
  • 1
    I think you lacking basic concepts: i) rounded variable is not class variable so can be accessed only in the function where it is defined. ii) Even it was a class variable, there would be no use of accessing that variable at viewDidLoad because this function is called before the value of rounded is set. – Shoaib Aug 08 '15 at 18:54
  • Thanks for your input! Lacking basic concepts, indeed. but still learning. so i'll get it eventually :-) - how would you fix my code to achieve the result i am looking for? – Joe Aug 08 '15 at 19:13

2 Answers2

0
  1. Declare an instance variable at the beginning of the class – outside any method – with the default value of the UISlider

    var rounded : Double = <defaultValueOfTheSlider>
    
  2. delete the keyword var before rounded in sliderValueChanged()

vadian
  • 274,689
  • 30
  • 353
  • 361
  • thank you for your help! we're getting closer, but rounded still has no effect on filter.setValue(rounded, forKey: kCIInputIntensityKey) inside of viewdidload. – Joe Aug 08 '15 at 19:11
  • as Shoaib mentioned `viewDidLoad()` is called very early – in any case before `sliderValueChanged()`, so you have to change the filter setting also in the `sliderValueChanged()` method – vadian Aug 08 '15 at 19:14
  • In theory, I now know why it isn't working as I intended (thanks to all you folks helping me out!) I created a function of all the code inside viewDidLoad, and i am calling it in sliderValueChanged. It works, however it's extremely inefficient because it's setting fileURL / beginImage / newImage everytime the slider moves. I just don't know how to adjust the code so it only effects the filter settings. Any code snippets would be greatly appreciated! – Joe Aug 08 '15 at 20:02
0

A few concepts that will help you out. The issue you are having is that you are creating an instance inside a function and it is lost when the function returns. The reason for this is that the functions data is created on the stack, the stack is temporary memory your app's process uses.

To access the variable throughout the class you can declare it at the top of your class. This will then be allocated to the heap so that it's available for later access until removed from the heap(deallocated).

Community
  • 1
  • 1
some_id
  • 29,466
  • 62
  • 182
  • 304