Xcode显示警告:Switch condition evaluates to a constant



`
import UIKit

enum Barcode
{
case UPCA(Int,Int,Int,Int)
case QRCode(String)
}

class ViewController: UIViewController,UITextViewDelegate
{
override func viewDidLoad()
{
var productBarcode = Barcode.UPCA(123, 456, 789, 0)
productBarcode = Barcode.QRCode("xyz")
//Xcode⚠️Switch condition evaluates to a constant
//警告:Switch 条件被判断为一个常量.
//可能是编译器认为变量在函数内部是不变的吧
switch productBarcode
{
case let .UPCA(a, b, c, d):
print(a,b,c,d)
case let .QRCode(str):
print(str)
}
}
}
`

解决方法:
将变量从函数内部提取出来,如下



`
import UIKit

enum Barcode
{
case UPCA(Int,Int,Int,Int)
case QRCode(String)
}

class ViewController: UIViewController,UITextViewDelegate
{
var productBarcode = Barcode.UPCA(123, 456, 789, 0)
override func viewDidLoad()
{
productBarcode = Barcode.QRCode("xyz")

    switch productBarcode
    {
    case let .UPCA(a, b, c, d):
        print(a,b,c,d)
    case let .QRCode(str):
        print(str)
    }
}

}
`

你可能感兴趣的:(Xcode显示警告:Switch condition evaluates to a constant)