我遇到问题,printf
样式格式与String(format:, ...)
一起工作,因为我希望它可以在Objective C或C中工作。
这是我的代码:
let name = "Bob"
let type = "Person"
let formattedString = String(format: "%*@", 30, "\(name)(\(type))")
print("\"\(formattedString)\"")
// Expected output: " Bob(Person)"
// Actual output: "Bob(Person)"
我做错了什么?
我也尝试过(同样出乎意料的结果):
let formattedString = String(format: "%30@", "\(name)(\(type))")
let formattedString = String(format: "%-30@", "\(name)(\(type))") // Padding on the other side
let formattedString = String(format: "%-*@", 30, "\(name)(\(type))")