0

Ideally I was expecting my absoluteURL to print

http://my-MacBook-Pro.local:8080/apis/v2/Driver/driverDetails?domain=123&driver=12&mykey=8908098

From this blog

But for some reason I am not able to get the full url defined correctly.

Code

var urlComponents = URLComponents(string : fullURL)!
for key in getData.allKeys {
   let valueOfKey : String = getData.value(forKey: key as! String) as! String
   print(key as! String + ":" + valueOfKey)
   urlComponents.queryItems?.append(URLQueryItem(name: key as! String, value : valueOfKey))
   //getData.value(forKey: key as! String) as! String?))
}

print("fullurl ->"+urlComponents.url!.debugDescription)

Current Output

domain:123 driver:12 mykey:8908098

fullurl->http://my-MacBook-Pro.local:8080/apis/v2/Driver/driverDetails

Kerberos
  • 4,036
  • 3
  • 36
  • 55
Siddharth
  • 9,349
  • 16
  • 86
  • 148

1 Answers1

4

urlComponents.queryItems will initially be nil, so urlComponents.queryItems?.append won't do anything. On the next iteration it is still nil, so again the append won't happen and so on.

You need to build your own array of URLQueryItem and just assign it once you are done:

var urlComponents = URLComponents(string : fullURL)!
var queryItems = [URLQueryItem]()

for key in getData.allKeys {
    let valueOfKey = getData.value(forKey: key as! String) as! String
    queryItems.append(URLQueryItem(name: key as! String, value : valueOfKey))
}

urlComponents.queryItems = queryItems
print("fullurl ->"+urlComponents.url!.debugDescription)
Siddharth
  • 9,349
  • 16
  • 86
  • 148
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • You should really look at your `getData` structure; there shouldn't be a need for so much force down casting. – Paulw11 Feb 25 '17 at 10:38
  • Once I get this to work consistently I'll refactor. For now, all is hell with this Swift nonsense. 1. the http url wont reach my localhost tomcat even though safari on the simulator can reach it. 2. this particular code just `Test Succeeded` at `let valueOfKey =` line. Does not execute the rest of the lines at all. Crazy, it worked two times and now its just crashing i think, leaving me with a log file full of unreadable nonsense.. :) – Siddharth Feb 25 '17 at 10:44
  • You have lots of force down casting; this is a recipe for crashes. Code defensively using conditional unwrapping. If your local tomcat server doesn't have a valid SSL certificate and you can't use SSL, make sure you have disabled ATS via your info.plist – Paulw11 Feb 25 '17 at 10:47
  • ` var parameters = [String:String]() parameters["domainOrGroupId"] = "5" parameters["driverId"] = "2" parameters["authKey"] = "ABCD" let queryItems = getData.map() {URLQueryItem.init(name: $0, value: $1)} urlComponents.queryItems = queryItems` also crashs.. – Siddharth Feb 25 '17 at 11:11
  • trying that info.plist thing.. not working. . but can only test after the test starts to work again – Siddharth Feb 25 '17 at 11:11
  • What is the crash? You probably want `URLQueryItem(name: $0, value: $1)`. – Paulw11 Feb 25 '17 at 12:28
  • I am trying to do what you recommended in http://stackoverflow.com/questions/24944625/loop-through-a-dictionary-in-swift – Siddharth Feb 25 '17 at 18:20