我试图扩展Character到符合Strideable以创建CountableClosedRange的Character类型。最后,我希望有这样的东西可以打印整个字母:
Character
Strideable
CountableClosedRange
("A"..."Z").forEach{ print($0) }
目前,我正在使用UnicodeScalar类型来计算两个字符之间的距离。因为Character类型无法使用标量,所以我需要根据Character创建一个String,获取第一个标量的值,然后计算它们之间的距离:
UnicodeScalar
extension Character: Strideable { func distance(to other: Character) -> Character.Stride { return abs(String(self).unicodeScalars.first?.value - String(other).unicodeScalars.first!.value) } func advanced(by n: Character.Stride) -> Character { return Character(UnicodeScalar(String(self).unicodeScalars.first!.value + n)) } }
即使这样,我Character仍然得到不符合协议Strideable和的错误_Strideable。编译器似乎没有选择Stride随附的关联类型Strideable:
_Strideable
Stride
public protocol Strideable : Comparable { /// A type that can represent the distance between two values of `Self`. associatedtype Stride : SignedNumber // ... }
我想念什么?
如前所述,由于a Character可以由多个unicode标量组成,因此您无法准确确定两个任意字符之间有多少个不同的有效字符表示形式,因此不是符合的理想选择Stridable。
Stridable
一种方法是你的只是想打印出字母表问题是顺应UnicodeScalar,而不是Character到Stridable-让你与由单一的Unicode代码点表示的字符工作,并推动它们基于代码点。
extension UnicodeScalar : Strideable { public func distance(to other: UnicodeScalar) -> Int { return Int(other.value) - Int(self.value) } /// Returns a UnicodeScalar where the value is advanced by n. /// /// - precondition: self.value + n represents a valid unicode scalar. /// public func advanced(by n: Int) -> UnicodeScalar { let advancedValue = n + Int(self.value) guard let advancedScalar = UnicodeScalar(advancedValue) else { fatalError("\(String(advancedValue, radix: 16)) does not represent a valid unicode scalar value.") } return advancedScalar } }
现在,您可以形成一个CountableClosedRange<UnicodeScalar>,并且可以将每个元素自由转换为Character或,String如果需要的话:
CountableClosedRange<UnicodeScalar>
String
("A"..."Z").forEach { // You can freely convert scalar to a Character or String print($0, Character($0), String($0)) } // Convert CountableClosedRange<UnicodeScalar> to [Character] let alphabetCharacters = ("A"..."Z").map {Character($0)}