生成一组键控数组:更有效的方法?

时间:2016-08-12 16:16:01

标签: ios arrays swift

我想对一组表演者进行排序,以便按照名字的第一个字符对它们进行分组。因此,例如,以下输出中的“A”表示首字母以“A”开头的表演者集合。

[
"A"[Performer,Performer,Performer,Performer]
"B"[Performer,Performer,Performer]
"C"[Performer,Performer,Performer]
"D"[Performer,Performer,Performer]
"F"[Performer,Performer,Performer]
"M"[Performer,Performer,Performer]
... etc
]

我已经实现了它,但我希望有更简单的方法来实现它。以下是我如何实现它。

class Performer {

    let firstName: String
    let lastName: String
    let dateOfBirth: NSDate

    init(firstName: String, lastName: String, dateOfBirth: NSDate) {
        self.firstName = firstName
        self.lastName = lastName
        self.dateOfBirth = dateOfBirth
    }

}

private var keyedPerformers: [[String: [Performer]]]?

init() {
    super.init()
    let performers = generatePerformers()
    let sortedPerformers = performers.sort { $0.0.firstName < $0.1.firstName }
    keyedPerformers = generateKeyedPerformers(sortedPerformers)
}

//'sortedPerformers' param must be sorted alphabetically by first name
private func generateKeyedPerformers(sortedPerformers: [Performer]) -> [[String: [Performer]]] {
    var collectionKeyedPerformers = [[String: [Performer]]]()
    var keyedPerformers = [String: [Performer]]()
    var lastLetter: String?
    var collection = [Performer]()
    for performer in sortedPerformers {
        let letter = String(performer.firstName.characters.first!)
        if lastLetter == nil {
            lastLetter = letter
        }
        if letter == lastLetter {
            collection.append(performer)
        } else {
            keyedPerformers[lastLetter!] = collection
            collectionKeyedPerformers.append(keyedPerformers)
            keyedPerformers = [String: [Performer]]()
            collection = [Performer]()
        }
        lastLetter = letter
    }
    return collectionKeyedPerformers.sort { $0.0.keys.first! < $0.1.keys.first! }
}

1 个答案:

答案 0 :(得分:0)

首先,由于你有一个(排序的)部分索引标题数组,因此没有必要使用字典数组。最有效的方法是按键从字典中检索Performers数组。

  • initialLetter类中添加一个惰性实例化变量Performer。我还添加了description属性以获得更具描述性的字符串表示。

    class Performer : CustomStringConvertible{
    
      let firstName: String
      let lastName: String
      let dateOfBirth: NSDate
    
      init(firstName: String, lastName: String, dateOfBirth: NSDate) {
        self.firstName = firstName
        self.lastName = lastName
        self.dateOfBirth = dateOfBirth
      }
    
      lazy var initialLetter : String = {
        return self.firstName.substringToIndex(self.firstName.startIndex.successor())
      }()
    
      var description : String {
        return "\(firstName) \(lastName)"
      }
    
  • 要创建表演者,请使用数组而不是简单的字符串。 &#34;失踪&#34;字母已被省略。

    private let createIndexTitles = ["A","B","C","D","E","F","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    
  • 创建虚拟实例的函数按initialLetter预先排序表演者数组,然后按lastName预先排序

    private func generatePerformers() -> [Performer] {
      var performers = [Performer]()
      for i in 0...99 {
        let x = i % createIndexTitles.count
        let letter = createIndexTitles[x]
        performers.append(Performer(firstName: "\(letter)", lastName: "Doe", dateOfBirth: NSDate()))
      }
      return performers.sort({ (p1, p2) -> Bool in
          if p1.initialLetter < p2.initialLetter { return true }
          return p1.lastName < p2.lastName
      })
    }
    
  • 现在为节标题索引

    创建一个空字符串数组
    private var sectionIndexTitles = [String]()
    
  • 创建键控表演者的功能遵循一个简单的算法:如果initialLetter的键不存在,请创建它。然后将执行者追加到数组中。最后将字典的排序键分配给sectionIndexTitles,键控数组的元素已经排序。

    private func generateKeyedPerformers(sortedPerformers: [Performer]) -> [String: [Performer]] {
      var keyedPerformers = [String: [Performer]]()
      for performer in sortedPerformers {
        let letter = performer.initialLetter
        var keyedPerformer = keyedPerformers[letter]
        if keyedPerformer == nil {
          keyedPerformer = [Performer]()
        }
        keyedPerformer!.append(performer)
        keyedPerformers[letter] = keyedPerformer!
      }
      sectionIndexTitles = Array(keyedPerformers.keys).sort { $0 < $1 }
      return keyedPerformers
    }
    
  • 现在测试一下

    let performers = generatePerformers()
    let keyedPerformers = generateKeyedPerformers(performers)
    print(sectionIndexTitles , keyedPerformers)
    
  • 在(推测)表视图中,使用sectionIndexTitles作为section数组,分别按键获取表演者数组。