【问题标题】:Expression type is ambiguous without more context for Array of arrays of typealias对于类型别名数组的数组,表达式类型不明确,没有更多上下文
【发布时间】:2016-01-03 13:49:00
【问题描述】:

我正在尝试创建一个 typealias 对象数组的数组 - 但我得到一个“表达式类型在没有更多上下文的情况下不明确”的编译错误。 这是我的代码:

typealias TestClosure = ((message: String)->())
var testArray = [[TestClosure]](count: 4, repeatedValue: nil)

我在这里错过了什么?

更新:

事实证明,当数组数组是一个可选数组时,它会编译:

var testArray = [[TestClosure]?](count: 4, repeatedValue: nil)

但是 - 我想确保初始数组将保存实际的空数组。 我该怎么做?

【问题讨论】:

    标签: arrays xcode swift swift2


    【解决方案1】:

    斯威夫特 2

    使用repeatedValue 创建空数组:

    var testArray = Array(count: 4, repeatedValue: [TestClosure]())
    

    斯威夫特 3

    语法变了,但思路还是一样的:

    var testArray = Array(repeating: [TestClosure](), count: 4)
    

    【讨论】:

      【解决方案2】:

      使用[] 代替nil 创建空数组:

      斯威夫特 3:

      typealias TestClosure = (String) -> ()
      var testArray = [[TestClosure]](repeating: [], count: 4)
      

      斯威夫特 2:

      typealias TestClosure = ((message: String) -> ())
      var testArray = [[TestClosure]](count: 4, repeatedValue: [])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-10
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        • 2019-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多