簡體   English   中英

Swift:`類型'[String]'不符合協議'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'

問題:

  • 如何處理這個錯誤?
  • 有沒有更清潔/更好的方法可以即時關閉設備?
  • 看來我不太了解! 和? Swift中的類型,是否在某個地方有一個易於理解的教程?

謝謝!

此問題與您對可選控件的使用無關。 比這更簡單。

在操場上,查看[["1","2"], ["3","4"]][0] 它不是[[String]] (字符串數組的數組)。 這是[String] (數組數組中的第一個條目)。

但是在第二個示例中,您試圖將此值分配給[[String]]類型的變量。 只需將units類型更改為[String]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM