简体   繁体   English

'[NSObject]' 不能转换为 '[AnyObject]'

[英]'[NSObject]' is not convertible to '[AnyObject]'

I'm trying to create tableview where the arrays are being sorted and put in there respective sections.我正在尝试创建 tableview,其中正在对数组进行排序并将其放入相应的部分。 I followed this tutorial: http://www.yudiz.com/creating-tableview-with-section-indexes/我跟着这个教程: http : //www.yudiz.com/creating-tableview-with-section-indexes/

I managed to make the first one work where the tableview arrays are sorted even though the sections without data still appear.我设法使第一个工作在 tableview 数组被排序的地方,即使没有数据的部分仍然出现。

The second one is about solving the problem in which the sections without data still appear which did not work for me.第二个是关于解决仍然出现没有数据的部分的问题,这对我不起作用。

Upon following the second one, I could not run it because of this error在跟随第二个之后,由于此错误,我无法运行它

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

Here is my code:这是我的代码:

Model for contact:联系方式:

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

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

Extension for partitioning arrays into sorted subcategories将数组划分为已排序的子类别的扩展

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)
    }
}

Tuple for data source and the line which has the error数据源的元组和有错误的行

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

You're trying to force cast a tuple into an array of arrays.您正在尝试将元组强制转换为数组数组。

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

will return a tuple of type ([AnyObject], [String]) .将返回类型为([AnyObject], [String])的元组。

Also, you shouldn't be using AnyObject unless you really need something to be a class type.此外,您不应该使用AnyObject除非您确实需要将某些东西作为class类型。 You can re-write like this:你可以这样重写:

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)
    }
}

That way you could write MyContact as a struct and it would still work with this function.这样你就可以将MyContact写成一个struct ,它仍然可以使用这个函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM