With the evolution of Swift, we often comes to create strings from raw text which can be a pain. Properly escaping any quotes or backslash characters in the raw text is an exercise in frustration. Swift 5, introduced with Xcode 10.2, introduces a new syntax to make it easier to work with raw text. Traditional Strings With String Literals To create a String from literal text, we use the common method of double quote (") as a starting and ending delimiter and the backslash (\) to escape special characters. For example, to create a String that preserves the double-quotes in this text: let title1 = "This is \"title\" here" // This is "title" here Swift 5 Custom String Escaping Swift 5 allows you to customize the delimiter and escape sequences. This is useful when working with raw text that might contain the delimiters or multiple escape sequence. Here we can pad the start, end and escape delimiters with one or more “ # ” characters....
Comments
Post a Comment