0

I have this string:

dataSourceURL = URL(string:"https://api.abc.com/api/p4/products?pid=uid8225&format=json&offset=\(count)&limit=20")

when I do print(dataSourceURL) I get:

https://api.abc.com/api/p4/products?pid=uid8225&format=json&offset=Optional(0)&limit=20

How can I remove ()? Please suggest.

I want to get

https://api.abc.com/api/p4/products?pid=uid8225&format=json&offset=0&limit=20 
Leandro Caniglia
  • 14,495
  • 4
  • 29
  • 51
Guri S
  • 1,083
  • 11
  • 12

1 Answers1

1

You can unwrap the value.

I would recommend using ?? as it will provide a default value and avoid the crash if it finds a nil.

let dataSourceURL = URL(string:"https://api.abc.com/api/p4/products?pid=uid8225&format=json&offset=\(count ?? 0)&limit=20")

it prints:

https://api.abc.com/api/p4/productspid=uid8225&format=json&offset=0&limit=20

Another way to do it is:

let dataSourceURL = URL(string:"https://api.abc.com/api/p4/products?pid=uid8225&format=json&offset=\(count!)&limit=20")

Be aware if count value is nil, the application will crash in this case.

Rahul
  • 2,056
  • 1
  • 21
  • 37