How to compare Rangelt;String.Indexgt; and DefaultBidirectionalIndiceslt;String.CharacterViewgt;?(如何比较 Rangelt;String.Indexgt;和 DefaultBidirectionalIndiceslt;String.CharacterViewgt;?)
问题描述
这种比较在 Swift 2 中有效,但在 Swift 3 中不再适用:
This comparison worked in Swift 2 but doesn't anymore in Swift 3:
让 myStringContainsOnlyOneCharacter = mySting.rangeOfComposedCharacterSequence(at: myString.startIndex) == mySting.characters.indices
如何比较 Range 和 DefaultBidirectionalIndices?
How do I compare Range and DefaultBidirectionalIndices?
推荐答案
来自 SE-0065 - 集合和索引的新模型
在 Swift 2 中,collection.indices
返回了一个 Range
,但是因为范围是一对简单的索引,并且索引不能再在它们的拥有,Range
不再是可迭代的.
In Swift 2,
collection.indices
returned aRange<Index>
, but because a range is a simple pair of indices and indices can no longer be advanced on their own,Range<Index>
is no longer iterable.
为了保持上述代码正常工作,Collection
获取了一个始终可迭代的关联 Indices 类型,...
In order to keep code like the above working, Collection
has acquired an associated Indices type that is always iterable, ...
由于 rangeOfComposedCharacterSequence
返回一个 range字符索引,解决方案是不使用indices
,但是startIndex..
Since rangeOfComposedCharacterSequence
returns a range of
character indices, the solution is not to use indices
, but
startIndex..<endIndex
:
myString.rangeOfComposedCharacterSequence(at: myString.startIndex)
== myString.startIndex..<myString.endIndex
这篇关于如何比较 Range<String.Index>和 DefaultBidirectionalIndices<String.CharacterView>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何比较 Range<String.Index>和 DefaultBidirectionalIndices<String.CharacterView>?
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01