7

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.

3
  • Where's the actual quote...? Btw you can just use toString) on float/double, no need for String(format:)
    – Alexander
    Jun 6, 2016 at 21:28
  • And you don't need to escape ', just type it inside the string and use interpolation for your variables.
    – Eric Aya
    Jun 6, 2016 at 21:29
  • did you solve your problem?
    – Azat
    Jan 17, 2020 at 11:07

2 Answers 2

4

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"
0
1

In Swift 5, below code worked for me

let myText = #"'"#
print(myText) 

Output

'

enter image description here

1
  • 2
    There's no need to use raw strings in this case. Single quotes no longer have to be escaped. let singleQuote = "'" works just fine. Apr 20, 2020 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.