Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 如何使用indexOf获取所选NSCollectionView的索引?_Macos_Swift_Indexing_Xcode7_Nscollectionview - Fatal编程技术网

Macos 如何使用indexOf获取所选NSCollectionView的索引?

Macos 如何使用indexOf获取所选NSCollectionView的索引?,macos,swift,indexing,xcode7,nscollectionview,Macos,Swift,Indexing,Xcode7,Nscollectionview,我在Objective-C中看到了一些示例,例如,但是NSCollectionView。子视图使用了一个名为.indexOfObject的属性,我假设该属性已被.indexOf替换,但我不确定如何使用.indexOf 目标C中的上下文 NSInteger index = [[myCollectionView subviews] indexOfObject:self]; Swift版本 var index = collectionView.subviews.indexOf(element: S

我在Objective-C中看到了一些示例,例如,但是
NSCollectionView。子视图
使用了一个名为
.indexOfObject
的属性,我假设该属性已被
.indexOf
替换,但我不确定如何使用
.indexOf

目标C中的上下文

NSInteger index = [[myCollectionView subviews]  indexOfObject:self];
Swift版本

var index = collectionView.subviews.indexOf(element: Self.Generator.Element)
问题:


如何使用
indexOf
获取所选
NSCollectionView
的索引?

使用
indexOf
非常简单:

let label = UILabel()
let tableView = UITableView()

let subviews: [UIView] = [label, tableView]

print(subviews.indexOf(label))     // Optional(0)
print(subviews.indexOf(tableView)) // Optional(1)
如您所见,如果在数组中找不到该对象,它将返回一个可选值
nil
。您可以像这样展开实际索引

if let index = collectionView.subviews.indexOf(self) {
    // do your stuff
} else {
    // view not found in subviews
}

这同样适用于OS X NSCollectionView吗?@DanBeaulieu如果您链接的问题中的Objective-C代码有效,则是。方法
indexOf
属于
CollectionType
协议,独立于
NSCollectionView