Juri
Juri

Reputation: 93

How to create url from int array

I am trying to create an URL something like it https://api.website.com/products?product_ids=1,2,3,4,5 Using integer array from CoreData

So far I do have this code

let array = [1,2,3,4,5]

var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "api.website.com"
urlComponents.path = "/products"
urlComponents.queryItems = [
    URLQueryItem(name: "product_ids", value: ???????),
]

How can I add my array data to get a full URL I needed? https://api.website.com/products?product_ids=1,2,3,4,5

Upvotes: 0

Views: 649

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236260

You just need to map your array elements into a string and join the result using a comma:

let value = array.map(String.init).joined(separator: ",")

Upvotes: 5

Related Questions