I am trying to use the old printf-style string formatters to define a new Swift string from an old Swift string.
I notice this works fine as long as I am starting with a Swift string literal, but not a string variable.
String(format:"%s", "hello world".cStringUsingEncoding(NSUTF8StringEncoding))
// ERROR: argument type '[CChar]?' does not conform to expected type 'CVarArgType'
let s = "hello world"
Don't create a C string just for alignment. There is a method stringByPaddingToLength for that.
// Swift 2 s.stringByPaddingToLength(10, withString: " ", startingAtIndex: 0) // Swift 3 s.padding(toLength: 10, withPad: " ", startingAt: 0)
Note that it will truncate the string if it is longer than 10 UTF-16 code units.
The problem here is, there are two methods in Swift named
func cStringUsingEncoding(encoding: UInt) -> UnsafePointer<Int8>, as a method of NSString
func cStringUsingEncoding(encoding: NSStringEncoding) -> [CChar]?, as an extension of String
If we want a pointer, we need to ensure we are using an NSString, not a String.
In the first example, a string literal can be a String or NSString, so the compiler chooses NSString since the other one won't work.
But in the second example, the type of
s is already set to String, so the method that returns
[CChar]? is chosen.
This could be worked-around by forcing
s to be an NSString:
let s: NSString = "hello world" String(format:"%s", s.cStringUsingEncoding(NSUTF8StringEncoding))