-2

I am using Xcode7.3 with Swift2.2.

I want to append an Array in url request.For example my array like [“jeevan”,”jeejo”]. I want to append this array with key(arrayKey) in url request like must be the following pattern

https://api.com/pre/ws/ch/roo?arrayKey=jeevan%2Cjeejo

How to solve this issue? Please help me

Nirav D
  • 65,660
  • 11
  • 144
  • 172
IKKA
  • 4,963
  • 5
  • 40
  • 75

3 Answers3

3

You need to use encode your URL instead of join Array with separator, but if you want to merge Array with URL you can try like this.

let str = ["jeevan","jeejo"]
let join = str.joinWithSeparator("%2C")
let url = "https://api.com/pre/ws/ch/roo?arrayKey=\(join)"

If you want to encode url encode this way.

let str = ["jeevan","jeejo"]
let join = str.joinWithSeparator(",") 
let url = "https://api.com/pre/ws/ch/roo?arrayKey=\(join)"
let encoded = url.stringByAddingPercentEncodingWithAllowedCharacters(.URLFragmentAllowedCharacterSet())

Note : The reason I have used , is because %2C is encode for , you can confirm it here on W3School URL Encoding.

Nirav D
  • 65,660
  • 11
  • 144
  • 172
0

easy solution can be like this

var URIString = ""

for item in array {
    URIString +=\(item)%2C
}

after subtract last 3 characters and make URL string

0

Simple code like this

var array: [String] = ["jeevan","jeejo"]

var myString = ""

for i in 0..<array.count {
   myString += array[i]
   if (i+1)<array.count { mystring+="%2C" }
}

Can give you result like this:

jeevan%2Cjeejo

Achron
  • 107
  • 5