swift - 具有特定 Self 类型的 Swift 协议扩展
问题描述
我为 UnsignedInteger 协议添加了一个扩展,以添加一个以十六进制格式表示数字的十六进制方法。我还希望特定的符合结构具有参数的默认值。我写的是下面的。
extension UnsignedInteger {
func hex(withFieldWidth fieldWidth: Int, andUseUppercase uppercase: Bool = true) -> String {
return String(format: "%0\(fieldWidth)\(uppercase ? "X" : "x")", self as! CVarArg)
}
}
extension UnsignedInteger where Self == UInt8 {
func hex(withFieldWidth fieldWidth: Int = 2, andUseUppercase uppercase: Bool = true) -> String {
// should call the UnsignedInteger implementation with the default parameters
return hex(withFieldWidth: fieldWidth, andUseUppercase: uppercase)
}
}
extension UnsignedInteger where Self == UInt16 {
func hex(withFieldWidth fieldWidth: Int = 4, andUseUppercase uppercase: Bool = true) -> String {
// should call the UnsignedInteger implementation with the default parameters
return hex(withFieldWidth: fieldWidth, andUseUppercase: uppercase)
}
}
但是,对于 UInt8 和 UInt16 特定的扩展,它似乎是在调用自身而不是第一个扩展块中的十六进制,正如我收到的 UInt8 和 UInt16 块的警告消息所解释的那样All paths through this function will call itself
:
如果我fieldWidh
从 UInt8 和 UInt16 块中删除 ,调用十六进制(带有硬编码值fieldWidth
)似乎编译得很好,我相信这样它是从第一个扩展块调用十六进制方法。下面是编译良好的代码。
extension UnsignedInteger {
func hex(withFieldWidth fieldWidth: Int, andUseUppercase uppercase: Bool = true) -> String {
return String(format: "%0\(fieldWidth)\(uppercase ? "X" : "x")", self as! CVarArg)
}
}
extension UnsignedInteger where Self == UInt8 {
func hex(andUseUppercase uppercase: Bool = true) -> String {
// should call the UnsignedInteger implementation with the default parameters
return hex(withFieldWidth: 2, andUseUppercase: uppercase)
}
}
extension UnsignedInteger where Self == UInt16 {
func hex(andUseUppercase uppercase: Bool = true) -> String {
// should call the UnsignedInteger implementation with the default parameters
return hex(withFieldWidth: 4, andUseUppercase: uppercase)
}
}
在进行协议扩展时,有没有办法为特定的符合结构的参数指定默认值?
解决方案
在进行协议扩展时,有没有办法为特定的符合结构的参数指定默认值?
您已经在问题中强调了这种方法的问题。
我怎样才能以不同的方式解决它?
UnsignedInteger
从那里继承BinaryInteger
可以为您提供bitWidth
信息(UInt8
=>8
,UInt16
=>16
等等)。
extension UnsignedInteger {
func hex(uppercase: Bool = true) -> String {
let fieldWidth = self.bitWidth / 4
return String(format: "%0\(fieldWidth)\(uppercase ? "X" : "x")", self as! CVarArg)
}
}
以上使其适用于UInt
, UInt8
, UInt16
, UInt32
& UInt64
。
更进一步,您可以使用FixedWidthInteger
& 现在它适用于所有有符号和无符号整数。
推荐阅读
- ios - 如何向 iOS 应用程序(cordova-plugin-firebasex)添加推送通知?
- java - Anylogic - 使用字符串值获取对象
- php - ErrorException 未定义变量 $siswa 在哪里
- android - 如何从 C++ 原生服务中查询 android 音频系统
- python-3.x - 当用户为变量 resp_2 输入 N 或 N 时,从两个循环中跳出
- visual-studio-code - VS Code 中是否有任何方法可以获取 IntelliJ 的行删除行为?
- go - 在漏桶算法中,当队列未满时,实现固定速率的正确逻辑是什么?
- postgresql - 如何使用带有 GORM 的 golang 光纤编程语言获取 postgresql 数据库中的最新数据?
- c# - 无法识别的 chrome 选项:AcceptInsecureCertificates 同时将 ChromeOptions 传递给 AppiumOptions - C# Selenium
- angular - 如何在主机应用程序的移动布局中查看时显示 Power BI 嵌入报表的多个页面