Swift gets more stable day by day!. You can use # delimiter with start and end of the strings to use less escape sequences. So now you can reduce usage of backslash in your code.
Let’s dive in code, for example, You want to print string look like “\Hi \Niks”, In traditional way :
let traditionalWayStr = “\\Hi \\Niks”
print(traditionalWayStr)
Output is : \Hi \Niks
Using hashTag in Swift 5
let hashTagString = #”\Hi \Niks”#
print(hashTagString)
Output is : \Hi \Niks
Now, if you want to print variable value in rawString then
let author = “- MdNiks”
let quote = #”Trust, but verify. \#(author)!”#
print(quote)
Output is : Trust, but verify. — MdNiks!
Power of HashTag in Regular Exp.
It’s really tough to use regular exp. in swift because Regular exp. has (\) backslash as contain right.
If you are define simple regular exp.
let regExp = try NSRegularExpression(pattern: “\([^)])”)
Error is : 1. Expected expression in container literal
2. Expected expression after unary operator
But Using (#) hashTag you can achieve easily and fast.
let regExp = try NSRegularExpression(pattern: #”\\\([^)]+\)”#)
print(regExp)
Output is : <NSRegularExpression: 0x600002ea7ed0> \\\([^)]+\) 0x0
Just use it. w00t! How cool is that and all done with just
a few lines of code!
If you have any comments or questions, please respond below! I’d love to hear from you.