为什么 Xcode 无法识别 swift 数组中的 indexOfObjectIdenticalTo?

NSMoron

我正在尝试翻译一些 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 中,这是作为Foundation.NSArray.indexOfObjectIdentical(to:). 这在 上不可用Swift.Array,因此您首先必须将您的 桥Array接到NSArray,因此将拼写为:

let textContainers = layoutManager.textContainers
let lastUsedContainerIndex = NSArray(textContainers).indexOfObjectIdentical(to: textContainer)

但在 Swift 中,这将更传统地写为:

let textContainers = layoutManager.textContainers
let lastUsedContainerIndex = textContainers.firstIndex(where: { $0 === textContainer })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么 Swift 无法识别 JS 发送的数组?

Xcode代码中无法识别的其他Swift标志

Swift中无法识别C指针数组

Xcode无法识别新的Swift类

Xcode为什么无法识别Main.storyboard?

为什么在scala中无法识别proc?

在Swift中,为什么GCD无法解析?

无法在数组中添加 json 键值,为什么在 swift 中

为什么无法从Xcode中的代码引用声音资产

为什么在Xcode 6中无法使用Assistant Preview?

为什么Playground中的Xcode 10无法自动运行?

为什么SwiftyJSON无法在Swift 3中解析数组字符串

Xcode,Swift,无法识别的单元测试类/方法

为什么无法识别 c++ 字符数组中的空终止

为什么Travis无法在脚本中识别已安装的CMake?

为什么Monkeypatch无法在构造函数中识别出赋值?

为什么 Dagger 无法识别 AppModule 中的 @Provides?

为什么R中的“ xts”无法识别我的日期序列?

为什么从node.js中无法识别“〜/”?

为什么剃刀中的 td 元素无法识别样式?

为什么在我的脚本中Dir无法识别为方法?

为什么在app.razor中无法识别NotAuthorizedContent标记

为什么Python无法识别集合中的元素?

为什么在“;”之后无法识别变量 在java中

为什么Prolog无法识别查询中的参数?

为什么无法识别 lambda 函数中的变量

Objective C / XCode-Xcode无法识别ImageView中的图像

为什么 Xcode 9.2 无法识别受信任的 IOS 9.3.5 设备?

无法在Swift中对协议UITableViewDataSource进行确认,为什么?