swift - 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: ©, 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: ©, replacement: String(replacementInt))
default:
self = format
}
}
self = copy
}
但是,应用 String(replacementInt) 时出现以下错误
协议类型“LosslessStringConvertible”不能符合“LosslessStringConvertible”,因为只有具体类型才能符合协议
奖励 如果我可以在不导入任何库并简单地使用 swift 编写的情况下做到这一点,那将是一个奖励。
解决方案
您可以使符合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'
推荐阅读
- node.js - CloudConvert 作业在导出时返回空结果,所有任务都列为等待
- ios - 为什么 swiftUI 中没有显示警报?
- powerbi - PowerBi 中的分类数据
- python-3.x - Django rest框架使用多对多提交的序列化程序创建对象
- c# - 字典中缺少 TryAdd() 的定义
- python - 人脸识别 - 每天更新参考人脸编码
- html - Ng-select 多选复选框禁用组
- java - AWS SDK Java DynamoDB - 使用表达式属性名称查询 - 未定义表达式中使用的表达式属性值
- npm - 通过 cmake execute_process 获取 npm --version
- xml - XML、XPath 和按属性值在同一级别上分组节点/属性