Error: Editor placeholder in source file when converting line extension to swift 3

subscript (r: Range<Int>) -> String { let start = startIndex.advancedBy(r.startIndex) let end = start.advancedBy(r.endIndex - r.startIndex) return self[Range(start: start, end: end)] } 

Fighting to convert the above index in my line extension to swift 3. The following shows what happened after I clicked the convert button on Xcode.

  subscript (r: Range<Int>) -> String { let start = characters.index(startIndex, offsetBy: r.lowerBound) let end = <#T##String.CharacterView corresponding to `start`##String.CharacterView#>.index(start, offsetBy: r.upperBound - r.lowerBound) return self[(start ..< end)] } 

Error screenshot

+5
source share
1 answer

All you have to do is add characters before the index. The compiler also gives you a hint to add String.CharacterView corresponding to start ##String.CharacterView . The message may be a little fuzzy, but it contains a lot of meaning! Tells you that an array of characters is expected. However, as @vadian suggests, you can even omit characters from the start.

I also wrote a little test just to make sure.

 import Foundation extension String { subscript (r: Range<Int>) -> String { let start = index(startIndex, offsetBy: r.lowerBound) let end = index(start, offsetBy: r.upperBound - r.lowerBound) return self[start..<end] } } let string = "Hello world" let range = Range(uncheckedBounds: (lower: 0, upper: 2)) let s = string[range] // prints "He" 
+4
source

Source: https://habr.com/ru/post/1261868/


All Articles