美文网首页
Xcode显示警告:Switch condition evalu

Xcode显示警告:Switch condition evalu

作者: 果啤 | 来源:发表于2016-08-19 17:00 被阅读605次

<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>

相关文章

网友评论

      本文标题:Xcode显示警告:Switch condition evalu

      本文链接:https://www.haomeiwen.com/subject/zeepsttx.html