Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single quote character in Swift

Tags:

swift

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.

like image 601
vkrishnan23 Avatar asked Jun 06 '16 21:06

vkrishnan23


People also ask

What is the char for single quote?

These are ASCII code 0x22 for double quotation mark, and ASCII code 0x27 for single quotation mark.

Can you use single quotes in XML?

XML Attributes Must be QuotedEither single or double quotes can be used.

How do you escape a single quote in Swift?

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.

How do I put single quotes in a 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 ( “\” ).


2 Answers

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"
like image 80
NSGangster Avatar answered Oct 17 '22 12:10

NSGangster


In Swift 5, below code worked for me

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

Output

'

enter image description here

like image 24
Shelly Pritchard Avatar answered Oct 17 '22 14:10

Shelly Pritchard