Reputation: 12670
Can one use (escaped) double quotes in a string interpolation in Swift?
let s = "\(n) \(capitalized ? "H" : "h")ours"
produces "Unexpected '"' character in string interpolation
(which is in line with a NOTE in the documentation), but I've also had no success with several attempts at escaping the inner double quotes so far.
So can one use (escaped) double quotes in string interpolations and if so how?
Upvotes: 1
Views: 2446
Reputation: 1891
Since Swift 2.1 we can use double quotes when interpolating, so the original code now works.
Upvotes: 2
Reputation: 2482
@Daniel answer is good, but in your case you could use the builtin capitalizedString method.
let s = "hours".capitalizedString
or
let s = "\n hours".capitalizedString
This method capitalize the first letter of each word. Edit:
let s = (capitalized ? "hours".capitalizedString : "hours")
Upvotes: 1
Reputation: 12015
You can escape characters with the \, but I think you couldn't use the ternary operator this way in string interpolation.
So I would suggest to put capitalized ? "H" : "h"
in a variable, and then it will work.
UPDATED
You can use the ternary operator in string interpolation, but you cannot use string literals in there, so you should put the whole expression to a variable, or the upper and the lowercase h.
Upvotes: 0