所以,让我们说我有一个字符串:" abc"我想改变每个角色的位置,这样我就能拥有" cab"后来" bca"。我希望索引0处的字符移动到1,索引1上的字符移动到2,索引2中的字符移动到0。
我有什么在Swift中这样做?另外,让我们说而不是字母,我有数字。有没有更简单的方法来使用整数?
答案 0 :(得分:1)
斯威夫特2:
extension RangeReplaceableCollectionType where Index : BidirectionalIndexType {
mutating func cycleAround() {
insert(removeLast(&self), atIndex: startIndex)
}
}
var ar = [1, 2, 3, 4]
ar.cycleAround() // [4, 1, 2, 3]
var letts = "abc".characters
letts.cycleAround()
String(letts) // "cab"
斯威夫特1:
func cycleAround<C : RangeReplaceableCollectionType where C.Index : BidirectionalIndexType>(inout col: C) {
col.insert(removeLast(&col), atIndex: col.startIndex)
}
var word = "abc"
cycleAround(&word) // "cab"