我只是在想:通常,“使用未解析的标识符”是什么意思?我通常会得到这个答案,有时我可以弄乱直到解决它,但是从来没有一个一致的答案。那么,这告诉我什么呢?
另外,这段代码不断抛出该错误,我被卡住了!我敢肯定这是 super 简单,我只是没有看到它!错误在“返回newImage”行上。感谢您的帮助!
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
“return newImage”行给了我这个问题。 (applyFilter函数的最后一行)
}
}
var filter = chooseFilter()
var editedImage = filter.applyFilter("warmImage")
最佳答案
简单来说, Unresolved 标识符错误意味着您使用的名称在当前作用域中不存在。
特别是,在这里您说的是return newImage
,但是在您所说的范围内没有变量newImage
(因为您在更深的范围不再声明了newImage
变量)。
因此,例如,当您说
if (filterSelection == "coldImage")
{
var newImage = callColor.turnBlue(255); callColor.turnGreen(255)
}
// no `newImage` here!
...在大括号内声明的
newImage
变量在右大括号后立即不复存在。