Trying to add a single quote '
character in my code in Swift, but it's constantly adding a \'
and this is necessarily for an API Call.
My code is:
let locationLat = String(format: "%f", (currentLocation?.coordinate.latitude)!)
let locationLong = String(format: "%f", (currentLocation?.coordinate.longitude)!)
let apiLocation = "$filter=geo.distance(Location,geographyPoint" + "(" + locationLat + locationLong + ")) le 0.1"
I need to make the apiLocation variable look like:
$filter=geo.distance(Location, geography'POINT(lat, long)') le 0.5&searchMode=all&$count=true
Let me know thanks.
These are ASCII code 0x22 for double quotation mark, and ASCII code 0x27 for single quotation mark.
XML Attributes Must be QuotedEither single or double quotes can be used.
To include quotes inside a string in Swift, escape the quote symbol with backslash character, i.e., place a backslash character before the double quote inside the string.
There are several ways to include quote characters within a string: A “'” inside a string quoted with “'” may be written as “''” . A “"” inside a string quoted with “"” may be written as “""” . Precede the quote character by an escape character ( “\” ).
Using escape character for single quotes \'
= '
and interpolation (\()
)for variables you can achieve this in one string.
let apiLocation = "$filter=geo.distance(Location, geography\'POINT(\(locationLat), \(locationLong))\' le 0.5&searchMode=all$count=true"
In Swift 5, below code worked for me
let myText = #"'"#
print(myText)
Output
'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With