Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use string/character literals within Swift string interpolation?

Tags:

swift

Is it possible to use a String/Character literal within string interpolation in Swift?

The language reference says:

The expression you write inside parentheses within an interpolated string cannot contain an unescaped double quote (") ...

That's a little fuzzy to me, since it seems to deliberately leave the loophole of escaped double quotes.

If I try:

println( "Output: \(repeat("H",20))" );

func repeat( char:Character, times:Int ) -> String {
    var output:String = "";
    for index in 1...times {
        output += char;
    }
    return output;
}

I get "Expected ',' separator".

Similarly, if I do the same thing, but escape the quotes, still no dice:

println( "Output: \(repeat(\"H\",20))" );

I suspect it's just not possible, and to be honest, no big deal -- I haven't found any example that I can't easily solve by doing a little work before the string interpolation, I guess I'm just looking for confirmation that it's just not possible.

like image 952
Geoffrey Wiseman Avatar asked Aug 11 '14 14:08

Geoffrey Wiseman


People also ask

What is string literal in Swift?

String Literals A string literal is a sequence of characters surrounded by double quotes, with the following form − "characters" String literals cannot contain an unescaped double quote ("), an unescaped backslash (\), a carriage return, or a line feed.

What character do you wrap interpolations with?

These interpolations are wrapped in ${} , such as ${var. foo} . The interpolation syntax is powerful and allows you to reference variables, attributes of resources, call functions, etc. You can perform simple math in interpolations, allowing you to write expressions such as ${count.

How do you replace a character in a string in Swift?

Replace characters with replacingCharacters method To get the range, we can simply use String 's built in range method. Next we can use replaceCharacters method. All we need to do is pass in the range that we have, and the string that want to use as the replacement string.

What is the difference between string and character literal?

What is the difference between character literals and string literals in Java? Character literals represents alphabets (both cases), numbers (0 to 9), special characters (@, ?, & etc.) and escape sequences like \n, \b etc. Whereas, the String literal represents objects of String class.

What is a string literal in Swift?

A string literal is a sequence of characters surrounded by double quotation marks ( " ). Use a string literal as an initial value for a constant or variable: Note that Swift infers a type of String for the someString constant because it’s initialized with a string literal value.

How to write an interpolated string in Swift?

) // Prints "Write an interpolated string in Swift using \ (multiplier)." To use string interpolation inside a string that uses extended delimiters, match the number of number signs after the backslash to the number of number signs at the beginning and end of the string.

What are substrings in Swift and how to use them?

Substrings in Swift have most of the same methods as strings, which means you can work with substrings the same way you work with strings. However, unlike strings, you use substrings for only a short amount of time while performing actions on a string. When you’re ready to store the result for a longer time,...

Are string and character comparisons in Swift locale-sensitive?

The characters are visually similar, but don’t have the same linguistic meaning: print ( "These two characters aren't equivalent." ) // Prints "These two characters aren't equivalent." String and character comparisons in Swift aren’t locale-sensitive.


1 Answers

It can be done starting in Swift 2.1: http://www.russbishop.net/swift-2-1

Prior to that, it wasn't possible.

like image 63
Geoffrey Wiseman Avatar answered Sep 20 '22 16:09

Geoffrey Wiseman