Converting String to URL return nil

Win
var url:String = "http://112.199.178.73:4000/contact" + "?" + ["accessToken": getAppDelegate().applicationData?.accessToken].queryStringWithEncoding();

var socketUrl = URL(string: url);
Andrew Carter

Don't build out URLs and URL strings manually like that.

var components = URLComponents()
components.scheme = "http"
components.host = "112.199.178.73"
components.port = 4000
components.path = "/contact"

// I'm assuming accessToken is a string here
if let accessToken = getAppDelegate().applicationData?.accessToken {
    components.queryItems = [
        URLQueryItem(name: "accessToken", value: accessToken)
    ]
}

guard let url = components.url else {
    fatalError("Failed to create URL")
}

If you could post the string that var url:String = "http://112.199.178.73:4000/contact" + "?" + ["accessToken": getAppDelegate().applicationData?.accessToken].queryStringWithEncoding(); generates we could tell you exactly what's going wrong, but I bet that however ["accessToken": getAppDelegate().applicationData?.accessToken].queryStringWithEncoding(); is getting appended to that string is going to give you an improper url string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting string to date returns nil

Converting double to string with a return

Converting a string into URL PHP

Swift 3.0 Converting String to Double returning nil?

Converting string to dictionary with url in the string

How to Return Nil String in Go?

string array not converting to url in swift

Converting URL to String and back again

Read string from a file return nil

Swift: Nil is incompatible with return type String

NSDateFormatter return (null) string when NSDate is nil

Convert String to NSURL is return nil in swift

Return nil vs zero length string

Ruby on rails return a string if a model attribute is nil

Converting String return method to jSON via Rest

Converting CMTime To String is wrong value return

Converting date string to desired date format and the output is nil

Converting String To NSDate Returned From Google Always Returns Nil

CryptoSwift - Converting UInt8 array to String resolves as nil

Swift 3: Converting Data to String returns a nil value

Getting nil from dateFromString when converting string to date

Found nil from dateFromString when converting string to date

How to check String value for nil before converting it to Double

Convert String to URL (Why is resulting variable nil)

URL(string:) returns nil for a path that contains cyrillic

Ruby: return the first character from string if it matches and if there is no match then return nil?

Making a recursive algorithm for converting URL string into JSON

Java - Return date from a string (NOT converting a string to a date)

how to return string requested in url?