【发布时间】:2020-03-25 20:03:57
【问题描述】:
我正在尝试翻译一些 Objective-C 代码:
NSArray *containers = [layoutManager textContainers];
NSUInteger lastUsedContainerIndex = [containers indexOfObjectIdenticalTo:textContainer];
到斯威夫特:
let textContainers = layoutManager.textContainers
let lastUsedContainerIndex = textContainers.indexOfObjectIdentical(to: textContainers)
其中 layoutManager 是 NSLayoutManager 类型。我在 Swift 代码的最后一行收到此错误:
“[NSTextContainer]”类型的值没有成员 'indexOfObjectIdenticalTo'
不明白这一点。我已经在文档中搜索了 Swift 并且 NSArray 有 indexOfObjectIdenticalTo 但它没有出现在 Xcode 的自动完成中并且给了我这个错误。是 Swift 的新手,想知道我在这里缺少什么?
【问题讨论】:
-
在 Swift 中,这被导入为
NSArray.indexOfObjectIdentical(to:),所以这将被拼写为:textContainers.indexOfObjectIdentical(to: textContainers)。但在 Swift 中,这会更传统地写成:textContainers.firstIndex(where: { $0 === textContainers })
标签: ios swift xcode macos nsarray