I am facing a weird issue with Swift +4.0 String. Here is a breakdown of the issue.
Let's assume we have a string:
var ourStr = "@p@P|Bl@[BACKSLASH]Fbk@XO[BACKSLASH]d@V[BACKSLASH][BACKSLASH]c@"
Here, we have 4 [BACKSLASH]
s in our String. Our task is to replace each occurrence of [BACKSLASH]
with a real backslash \
.
We start by figuring out how we will implement this in our AwesomePlayground.playground
:
ourStr = ourStr.replacingOccurrences(of: "[BACKSLASH]", with: "\\")
print(ourStr) // prints @p@P|Bl@\Fbk@XO\d@V\\c@
Works great, let's implement this in our AwesomeProject.xcodeproj
ourStr = ourStr.replacingOccurrences(of: "[BACKSLASH]", with: "\\")
print(ourStr) // prints @p@P|Bl@\\Fbk@XO\\d@V\\\\c@
This is completely unexpected! We just test this out in the playground and it's working perfectly fine!
In the xcodeproj it replaced the [BACKSLASH] with 2 backslashes, while in the playground it knows that one is escaping character.
I solved it by using NSString
replacingOccurrences
method. But, I believe there is something wrong going there.