NSSet没有下标成员

问题描述 投票:1回答:2

我有一个Core Data对象,其关系是NSSet。我正在尝试使用它作为我的dataSource,但得到以下内容

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = playerTableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell
    cell.textLabel?.text = self.game.players[indexPath.row]
    return cell
}

我如何在tableView中使用我的NSSet,我相信这将是无序的,所以每次都有可能不同,所以我怎么能订购这个集合?

swift uitableview nsset
2个回答
9
投票

您可以使用allObjects从集合中获取数组,然后对对象进行排序。

let orderedPlayers = (game.players!.allObjects as! [Player]).sort { $0.name < $1.name }

如果您将该集声明为本机Swift类型Set<Player> - CoreData也支持Swift类型 - 您甚至可以省略类型转换和allObjects

let orderedPlayers = game.players!.sort { $0.name < $1.name }

0
投票

这就是我做的方式:

for player in game.players! {
        orderedPlayers.append(player as! Player)
    }
    orderedPlayers.sortInPlace({ $0.name < $1.name })
© www.soinside.com 2019 - 2024. All rights reserved.