如何比较Range< String.Index>和DefaultBidirectionalIndices< String.CharacterView&gt ;? [英] How to compare Range<String.Index> and DefaultBidirectionalIndices<String.CharacterView>?

查看:83
本文介绍了如何比较Range< String.Index>和DefaultBidirectionalIndices< String.CharacterView&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此比较在Swift 2中有效,但在Swift 3中不再可用.

This comparison worked in Swift 2 but doesn't anymore in Swift 3:

let myStringContainsOnlyOneCharacter = mySting.rangeOfComposedCharacterSequence(at: myString.startIndex) == mySting.characters.indices

如何比较Range和DefaultBidirectionalIndices?

How do I compare Range and DefaultBidirectionalIndices?

推荐答案

来自

在Swift 2中,collection.indices返回了Range<Index>,但是由于范围是一对简单的索引,并且索引无法再自行扩展,因此Range<Index>不再可迭代.

In Swift 2, collection.indices returned a Range<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已获取始终可迭代的关联索引类型,...

In order to keep code like the above working, Collection has acquired an associated Indices type that is always iterable, ...

由于rangeOfComposedCharacterSequence返回的范围为 字符索引,解决方案是 not 不能使用indices,但是 startIndex..<endIndex:

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&lt; String.Index&gt;和DefaultBidirectionalIndices&lt; String.CharacterView&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆