Is there a way in Swift 3.0 to get a StaticString
type out of a String
type which is complex?
Example (needs a conversion to work):
let aString: StaticString = "One part" + "Second part"
This is not possible, because a StaticString
is defined as
A simple string designed to represent text that is "knowable at compile-time". (Reference)
String concatenation happens at runtime.
I don't know what you are planing to do, but you can do something like that:
func aMethod(i: Int) -> StaticString {
switch i {
case 0:
return "Simple"
case 1:
return "Complex"
default:
return "Default"
}
}
let result = aMethod(i: 1)
print("\(type(of: result)): \(result)") // prints "StaticString: Complex"
You can use format specifiers like %d
or %s
eg:
os_log("Successfully fetched %d recordings.", type: .info, count)
os_log("Getting recordings from %s.",
type: .info, url.absoluteString)
for more info check here
Depends on your actual use case, you may have some way to work it around. For example, if you are dealing with os_log
, you can use %@
if you want to use Swift String
instead of CVarArg
.
//os_log("Compiler error with: \(string).", log: log)
os_log("This is OK: %@.", log: log, string)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With