<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 條件被判斷為一個(gè)常量.
//可能是編譯器認(rèn)為變量在函數(shù)內(nèi)部是不變的吧
switch productBarcode
{
case let .UPCA(a, b, c, d):
print(a,b,c,d)
case let .QRCode(str):
print(str)
}
}
}
`
</code>
</pre>
解決方法:
將變量從函數(shù)內(nèi)部提取出來(lái),如下
<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>