【问题标题】:Cannot convert value of type '[CustomObject]' to expected argument type '[_]'无法将类型“[CustomObject]”的值转换为预期的参数类型“[_]”
【发布时间】:2017-02-02 10:17:35
【问题描述】:

这是类声明:

class VXDropdownFormRow<T: SelectableOption>: DropdownFormRow<T> where T: Equatable {
    override init(title: String?, options: [T], selectedOption: T?, cellSelection: FormCellSelectionClosureType?, valueChanged: ((FormRow) -> Void)?) {
        super.init(title: title, options: options, selectedOption: selectedOption, cellSelection: cellSelection, valueChanged: valueChanged)
        self.cellHeight = 74
    }
}

我是从另一个班级调用它:

formRow = VXDropdownFormRow(title: NSLocalizedString("xyz", comment: "").uppercased(), options: durationArray, selectedOption:contractDurationtItem , cellSelection: nil, valueChanged: { (row) in self.contract.duration = row.value as?合同期限 })

durationArray 是:

var durationArray : [ListItem]

符合协议SelectableOption,就像在类的声明中所说的那样:

open class ListItem: NSManagedObject, SelectableOption {
    func setValues(fromJSON json: JSON, sortOrder: Int) {
        defaultValue = json["default"].stringValue == "true" ?  (true) : (false)
        value = json["value"].stringValue
        text = json["text"].stringValue
        localSortOrder = (sortOrder as NSNumber?)
    }
    public func selectableOptionTitle() -> String {
        return self.text!
    }
    public func selectableOptionSectionTitle() -> String {
        return ""
    }

} 

但编译器在构建时抛出错误:

Cannot convert value of type '[ListItem]' to expected argument type '[_]'

我应该怎么做才能修复这个错误?

【问题讨论】:

  • 你试过VXDropdownFormRow&lt;ListItem&gt;(title: ...)吗?
  • 没有帮助。同样的错误。

标签: ios swift xcode generics swift3


【解决方案1】:

我通过修复文件中设置为VXDropdownFormRow&lt;OldClassName&gt; 的原始声明来解决此问题。请检查声明以确保一切正常。此错误具有误导性,并不能帮助您找到问题。

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 2016-07-02
    • 2016-03-21
    • 1970-01-01
    • 2020-03-01
    • 2016-08-01
    • 2017-02-07
    • 2016-06-03
    • 1970-01-01
    相关资源
    最近更新 更多