简体   繁体   English

使用 Swift 中的 selectedIndexSet 从 Cocoa NSOutlineView 获取多个项目

[英]Get multiple items from Cocoa NSOutlineView using selectedIndexSet in Swift

When I need to get the selected item from a NSOutlineView I can just use this:当我需要从 NSOutlineView 中获取所选项目时,我可以使用它:

func item(atRow row: Int) -> Any?

But how do I get a list of items when using multiple selections?但是在使用多项选择时如何获取项目列表? I can use我可以用

var selectedRowIndexes: IndexSet { get }

to get an IndexSet but isn't there an implemented way to get an array of the selected items?获取 IndexSet 但没有实现的方法来获取所选项目的数组?

Thanks for any advise感谢您的任何建议

Map the rows to the items, compactMap unwraps the optionals safely. Map到项目的行, compactMap安全地展开选项。

let selectedItems = outlineView.selectedRowIndexes.compactMap{outlineView.item(atRow: $0)}

outlineView is the reference to the NSOutlineView outlineView是对NSOutlineView的引用

The result is an array of Any .结果是一个Any数组。 You can cast the type either in the closure or at the end of the line.您可以在闭包中或在行尾强制转换类型。

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

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