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

<pre>
<code>
`
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)
}
}
}
`
</code>
</pre>

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

<pre>
<code>
`
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)
    }
}

}
`
</code>
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容