首页 > 解决方案 > Swift重写init(格式:字符串,_参数:CVarArg ...)

问题描述

我正在尝试重写在基础中找到的标准 String(format, arguments) 方法,该方法接受一个字符串并将所有包含 %i 的值替换为整数,将 %@ 替换为字符串和一系列类型。https://developer.apple.com/documentation/swift/string/3126742-init

由于我不知道 c 我将初始化程序从

init(format: String, _ arguments: CVarArg) {

init(format: String, _ arguments: [Any]) {

现在我在字符串扩展中使用它来为 Ints 工作

  init(format: String, _ arguments: [Any]) {
            var copy = format
            for argument in arguments {
                switch argument {
                case let replacementInt as Int:
                    String.handleInt(copy: &copy, replacement: String(replacementInt))
                default:
                    self = format
                }
            }
        self = copy
    }

private static func handleInt(copy: inout String, replacement: String) {

但是因为我希望它适用于所有值,所以我试图使用开关来查找具有使用 String(value) 初始化程序转换为字符串所需的 LosslessStringConvertible 协议的 Any 类型。

     init(format: String, _ arguments: [Any]) {
        var copy = format
        for argument in arguments {
            switch argument {
            case let replacementInt as LosslessStringConvertible:
                String.handleAnyValue(copy: &copy, replacement: String(replacementInt))
            default:
                self = format
            }
        }
    self = copy
}

但是,应用 String(replacementInt) 时出现以下错误

协议类型“LosslessStringConvertible”不能符合“LosslessStringConvertible”,因为只有具体类型才能符合协议

奖励 如果我可以在不导入任何库并简单地使用 swift 编写的情况下做到这一点,那将是一个奖励。

标签: swiftswitch-statementprotocolscontrol-flow

解决方案


您可以使符合LosslessStringConvertible成为参数的要求:

init<S: LosslessStringConvertible>(format: String, _ arguments: [S])

这将支持开箱即用符合该协议的所有类型(并允许扩展其他类型以符合该协议):

var x: String  = String(format: "%i %@", 5, "five")
print(x) // prints "5 five"

此解决方案的限制是,例如不符合 LosslessStringConvertible 的类型将导致错误。例如:

class Z {}
let z = Z()
var y: String = String(format: "%i %@ %@", 5, "five", z) // Compilation error: Argument type 'Z' does not conform to expected type 'CVarArg'

推荐阅读