pr0grammer
pr0grammer

Reputation: 11

"Use of Unresolved Identifier" Error on The Return in a Function

I was just wondering: in general, what does "use of unresolved identifier mean"? I commonly get this and I am sometimes able to mess around until I solve it but there is never a consistent answer. So what is it telling me?

Also, this piece of code keeps throwing that error and I am stuck! I'm sure it is super simple and I am just not seeing it! The error is on the "return newImage" line. Thank you for the help!

    class ColorBoost {

func turnRed(amount: Int) -> UIImage {
    for y in 0..<myRGBA.height {
        for x in 0..<myRGBA.width {
            let index = y * myRGBA.width + x
            var pixel = myRGBA.pixels[index]
            let redDiff = Int(pixel.red) - avgRed
            if (redDiff > 0)
            {
                pixel.red = UInt8( max(0,min(255,avgRed+redDiff*amount     ) ) )
                myRGBA.pixels[index] = pixel
            }
        }
    }

    let redFilteredImage = myRGBA.toUIImage()

    return redFilteredImage!

}

func turnBlue(amount: Int) -> UIImage {
    for y in 0..<myRGBA.height {
        for x in 0..<myRGBA.width {
            let index = y * myRGBA.width + x
            var pixel = myRGBA.pixels[index]
            let blueDiff = Int(pixel.blue) - avgBlue
            if (blueDiff > 0)
            {
                pixel.blue = UInt8( max(0,min(255,avgBlue+blueDiff*amount ) ) )
                myRGBA.pixels[index] = pixel
            }
        }
    }

    let blueFilteredImage = myRGBA.toUIImage()

    return blueFilteredImage!

    }

func turnGreen(amount: Int) -> UIImage {
    for y in 0..<myRGBA.height {
        for x in 0..<myRGBA.width {
            let index = y * myRGBA.width + x
            var pixel = myRGBA.pixels[index]
            let greenDiff = Int(pixel.green) - avgGreen
            if (greenDiff > 0)
            {
                pixel.green = UInt8(  max(0,min(255,avgGreen+greenDiff*amount ) ) )
                myRGBA.pixels[index] = pixel
            }
        }
    }

    let greenFilteredImage = myRGBA.toUIImage()

    return greenFilteredImage!

}

     }


      class chooseFilter {


func applyFilter(filterSelection: String) -> String {

    var callColor = ColorBoost()

    if (filterSelection == "coldImage")

    {
       var newImage = callColor.turnBlue(255); callColor.turnGreen(255)
    }

    if (filterSelection == "warmImage")

    {
        var newImage = callColor.turnRed(5); callColor.turnGreen(15)
    }

    if (filterSelection == "scaryImage")
    {
       var newImage = callColor.turnRed(-1); callColor.turnBlue(-1);   callColor.turnGreen(-1)
    }

    if (filterSelection == "mutedBlue")
    {
        var newImage = callColor.turnRed(-1); callColor.turnBlue(-1);   callColor.turnGreen(-1)
    }

    if (filterSelection == "green")
    {
        var newImage = callColor.turnGreen(2)
    }

 return newImage

the "return newImage" line is giving me the issue. (last line of the applyFilter function)

}

 }


 var filter = chooseFilter()
 var editedImage = filter.applyFilter("warmImage")

Upvotes: 0

Views: 1995

Answers (2)

Shamar Yarde
Shamar Yarde

Reputation: 761

Using matt's tip, you should create the newImage variable at the top of the function or at the beginning of the class (after the first curly bracket), then do the following within the applyFilter function:

    if (filterSelection == "coldImage")
{
   newImage = callColor.turnBlue(255); callColor.turnGreen(255)
}
return newImage

This way the variable will be known at the end of the function.

Upvotes: 0

matt
matt

Reputation: 535087

The unresolved identifier error means, simply, that you are using a name that doesn't exist in the current scope.

In particular, here you are saying return newImage, but there is no variable newImage in the scope where you are saying it (because you declared your newImage variable(s) at a deeper scope that no longer exists).

Thus, for example, when you say

if (filterSelection == "coldImage")
{
   var newImage = callColor.turnBlue(255); callColor.turnGreen(255)
}
// no `newImage` here!

...the newImage variable declared inside the curly braces ceases to exist immediately after the right curly brace.

Upvotes: 1

Related Questions