部分文字列を別の部分文字列に置き換えようとしていますが、このエラー:Swift 3: 'String.Index'型の値を期待される引数型に変換できませんRange(別名Range <String.CharacterView.Index>)
cannot convert value of type 'String.Index' (aka 'String.CharacterView.Index') to expected argument type 'Range<String.Index>' (aka 'Range<String.CharacterView.Index>')
ここでは私のコードです:
// This are the position of the substrings:
let rangeOne = strToSort.index(strToSort.startIndex, offsetBy: (i-1))
let rangeTwo = strToSort.index(strToSort.startIndex, offsetBy: (i))
Here is where I try to make the replacement:
strToSort = strToSort.replacingCharacters(in: rangeOne, with: strToSort.substring(with: rangeTwo))
しかし、私はこのエラーを取得しています:
あなたは、私が間違ってやっていることやこれを回避することを知っていますか?
私は何の範囲に定義はありませんあなたのコードのように
のように異なる
String
メソッドを使用できますか? –'var chars =" EHLLOAGAIN ".characters.map {文字列($ 0)}' 'swap(&chars [0]、&chars [1])' –
@LeoDabusですが、プログラムでどのように行うのですか?私が意味することは、部分文字列が位置yにある位置xにある部分文字列を切り替えるかどうかです。 – user2924482