collat​​ionStringSelector的iOS / Swift UILocalizedIndexedCollat​​ion方法部分不起作用

时间:2018-02-28 15:38:37

标签: ios swift selector

我在调用collationStringSelector的方法部分时遇到了一些问题。它找不到我已正确定义的选择器。

这是我的方法调用:

for element in elements {
    if let indexable = element as? CollationIndexable {
        let collationIndex = collation.section(for: indexable, collationStringSelector: "collationString")

        if contentCollationIndexed[collationIndex] == nil {
            contentCollationIndexed[collationIndex] = [Element]()
        }
        contentCollationIndexed[collationIndex]!.append(element)
    }
} 

这是我的元素类型应该实现的协议

@objc protocol CollationIndexable : class {
    @objc var collationString : String { get }
}

这是具体的元素类型实现协议和选择器属性

extension Contact : CollationIndexable {

    @objc var collationString : String {
        return lastName
    }
}

UPDATE!

好的,我已经解决了问题,Contact类必须继承表单

  

NSObject的

1 个答案:

答案 0 :(得分:2)

使用您的示例使this tutorial与Swift 4一起使用。

var objects: [CollationIndexable] = [] {
        didSet {
            sections = Array(repeating: [], count: collation.sectionTitles.count)
            let selector = #selector(getter: CollationIndexable.collationString)

            let sortedObjects = collation.sortedArray(from: objects, collationStringSelector: selector)
            for object in sortedObjects {
                let sectionNumber = collation.section(for: object, collationStringSelector: selector)
                sections[sectionNumber].append(object as AnyObject)
            }

            self.tableView.reloadData()
        }
    }

这是我的元素类型应该实现的协议

@objc protocol CollationIndexable {
    @objc var collationString : String { get }
}

这是具体的元素类型实现协议和选择器属性

extension Contact : CollationIndexable {

    @objc var collationString : String {
        return lastName
    }
}