【问题标题】:'[NSObject]' is not convertible to '[AnyObject]'“[NSObject]”不能转换为“[AnyObject]”
【发布时间】:2018-06-06 20:00:48
【问题描述】:

我正在尝试创建对数组进行排序并放入相应部分的表格视图。我遵循了这个教程:http://www.yudiz.com/creating-tableview-with-section-indexes/

我设法使第一个工作在 tableview 数组被排序的地方工作,即使没有数据的部分仍然出现。

第二个是关于解决没有数据的部分仍然出现这对我不起作用的问题。

在第二个之后,由于这个错误,我无法运行它

'[MyContact]' is not convertible to '[AnyObject]'

这是我的代码:

联系方式:

class MyContact: NSObject {
    @objc var name: String!
    @objc var mobile: String!

    init(name: String, mob: String) {
        self.name = name
        self.mobile = mob
    }
}

将数组划分为排序子类别的扩展

extension UILocalizedIndexedCollation {

    func partitionObjects(array: [AnyObject], collationStringSelector: Selector) -> ([AnyObject], [String]) {
        var unsortedSections = [[AnyObject]]()

        for _ in self.sectionTitles {
            unsortedSections.append([])
        }

        for item in array {
            let index: Int = self.section(for: item, collationStringSelector: collationStringSelector)
            unsortedSections[index].append(item)
        }
        var sectionTitles = [String]()
        var sections = [AnyObject]()
        for index in 0 ..< unsortedSections.count {
            if unsortedSections[index].count > 0 {
                sectionTitles.append(self.sectionTitles[index])
                sections.append(self.sortedArray(from: unsortedSections[index], collationStringSelector: collationStringSelector) as AnyObject)
            }
        }
        return (sections, sectionTitles)
    }
}

数据源的元组和有错误的行

let (arrayContacts, arrayTitles) = collation.partitionObjects(array: self.myContacts, collationStringSelector: #selector(getter: MyContact.name)) as! [[MyContact]]

【问题讨论】:

  • 将一行拆分成多个语句,你会看到错误。
  • 看起来您正试图将元组强制转换为数组数组。这是合法的命令吗?
  • @GuyKogus 哦,天哪,我不敢相信我错过了,我可能是盲目输入的。无论如何发表您的评论作为答案,我会接受它。

标签: ios uitableview swift4 xcode9 swift3.2


【解决方案1】:

您正试图将元组强制转换为数组数组。

let (arrayContacts, arrayTitles) = collation.partitionObjects(array: self.myContacts, collationStringSelector: #selector(getter: MyContact.name))

将返回一个 ([AnyObject], [String]) 类型的元组。

另外,你不应该使用AnyObject,除非你真的需要一个class 类型。你可以这样重写:

extension UILocalizedIndexedCollation {
    func partitionObjects(array: [Any], collationStringSelector: Selector) -> ([Any], [String]) {
        var unsortedSections = [[Any]](repeating: [], count: self.sectionTitles.count)

        for item in array {
            let index = self.section(for: item, collationStringSelector: collationStringSelector)
            unsortedSections[index].append(item)
        }
        var sectionTitles = [String]()
        var sections = [Any]()
        for index in 0..<unsortedSections.count {
            if unsortedSections[index].isEmpty == false {
                sectionTitles.append(self.sectionTitles[index])
                sections.append(self.sortedArray(from: unsortedSections[index], collationStringSelector: collationStringSelector))
            }
        }
        return (sections, sectionTitles)
    }
}

这样你可以把MyContact写成struct,它仍然可以使用这个函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多