【发布时间】:2015-04-21 03:09:34
【问题描述】:
我想使用 appendFormat 将字符串附加到 NSMutableString,插入空格以获得字符串的最小长度。
在objective-c中,我只是用过
[text.mutableString appendFormat:@"%-12s", "MyString"];
我会得到
"MyString "
但在 Swift 中,我尝试过
text.mutableString.appendFormat("%-12s", "MyString")
我得到了一切,但不是“MyString”。出现了一些我不知道它来自哪里的随机字符。
有没有人知道为什么会发生这种情况,我应该怎么做?
谢谢你们!
【问题讨论】:
-
我可以在 Playground 中重现这一点,每次运行代码时随机字符都会发生变化。使用
str += String(format:"%-12s", "Hello")和变体。
标签: ios xcode swift nsmutablestring