![](/img/trans.png)
[英]Type anyObject? does not conform to 'StringLiteralConvertible'
[英]Swift: `Type '[String]' does not conform to protocol 'StringLiteralConvertible'`
我正在玩Swift和Xcode。 這適用於游樂場:
[["1","2"], ["3","4"]][0]
但這在Xcode項目中不起作用:
enum UnitSystem: Int {
case Standard = 0
case Metric = 1
}
class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate {
@IBOutlet weak var unitSystemControl: UISegmentedControl! //"Standard" and "Metric"
var units: [[String]]! = nil
var currentUnitSystem: UnitSystem! = nil
override func viewDidLoad() {
super.viewDidLoad()
...
currentUnitSystem = UnitSystem(rawValue: unitSystemControl.selectedSegmentIndex)
units = [["ft.", "in."], ["m.", "cm."]][currentUnitSystem.rawValue]
}
...
...
}
我收到此錯誤: Type '[String]' does not conform to protocol 'StringLiteralConvertible'
謝謝!
此問題與您對可選控件的使用無關。 比這更簡單。
在操場上,查看[["1","2"], ["3","4"]][0]
。 它不是[[String]]
(字符串數組的數組)。 這是[String]
(數組數組中的第一個條目)。
但是在第二個示例中,您試圖將此值分配給[[String]]
類型的變量。 只需將units
類型更改為[String]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.