Split let url = *** into ServIP, Port, xmlcommand

Hi Forum

I have several "function(s)" like this code below that open different environments.but I would like to separate the following line into 2 variables, as here below.

let url = URL(string: "http...://192.168.0.196:8000/xmlcommand")!

ServIP = 192.168.0.196
Port = 8000

So that the ServIP can be put into a "user" typed input Configuration field in my App so user can add different IP's for different systems this input field is asks users to enter the ServIP, and Port in this format "192.168.0.196:8000 so these can be reused in these functions, instead of hardcoded IP's.

New string in the code should then look like this:

let url = URL(string: "http...://ServIP:Port/xmlcommand")!

I have been trying to split these up in some let lines but can't get it right. anyone can tell me how to split these into variables?

Code Block
        // Open Environment called "Environment_Name"
func sendHttpPostAnimals() {
       let session = URLSession(configuration: .default)
       let url = URL(string: "http://192.168.0.196:8000/xmlcommand")!
       var request = URLRequest(url: url)
       request.httpMethod = "POST"
       request.setValue("text/plain", forHTTPHeaderField: "Content-Type") // I guess this can be "text/xml"
       // Working line
       request.httpBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Commands><command type=\"open\"><name>environments/Animals.hwe</name><id>a1</id></command></Commands>".data(using: .utf8)
       let task = session.dataTask(with: request) { data, response, error in
               //    print(data as Any)
              //    print(response as Any)
               //    print(error as Any)
               // do something with the result
               print(data as Any? as Any)
               if let data = data {
                       print(String(data: data, encoding: .utf8)!)
               } else {
                       print("no data")
               }
       }
       task.resume() // <- otherwise your network request won't be started


Br
Enevold

Answered by workingdogintokyo in 644419022
it is common practice to use the string interpolation \(...) , such as this:
Code Block
let servIp = "192.168.0.196" // String
let port = 8000 // Int
let cmd = "xmlcommand" // String
if let url = URL(string: "http://\(servIp):\(String(port))/\(cmd)") {
print("----> url: \(url)")
}
Accepted Answer
it is common practice to use the string interpolation \(...) , such as this:
Code Block
let servIp = "192.168.0.196" // String
let port = 8000 // Int
let cmd = "xmlcommand" // String
if let url = URL(string: "http://\(servIp):\(String(port))/\(cmd)") {
print("----> url: \(url)")
}
Hi Again

Sorry about it but I just really started on SwiftUI a few days ago so I feel pretty much newbie ;-)
but this app I'm trying to build teaches me new stuff each day also I think I used quite some time to try figure this one out,
I probably need to dig into some more basics as well, but for now I need to work on this project.

Thank you for your suggestion, I will try it out.

Br.
Michael
Exactly the same, but a string composition that may be easier to read… Just a question of taste.

Code Block
let service = "http://" // could be https
let servIp = "192.168.0.196" // String
let port = 8000 // Int
let cmd = "xmlcommand" // String
let url = service + servIp + ":" + String(port) + "/" + cmd
print("url", url)

You should better consider adopting URLComponents.
Code Block
let servIp = "192.168.0.196"
let port = 8000 //<- You may need to convert `portTextField.text` to `Int`
let command = "xmlcommand"
var urlComponents = URLComponents()
urlComponents.scheme = "http"
urlComponents.host = servIp
urlComponents.port = port
urlComponents.path = "/\(command)"
if let url = urlComponents.url {
print(url) //->http://192.168.0.196:8000/xmlcommand
//Use `url` here
//...
} else {
print("Bad component for URL: \(servIp), \(port), \(command)")
//Do something appropriate for error case
}

URLComponents will make you free from some difficulties like escaping.
Hi Guys

Thank you for your great suggestions, I will test each to see which is the easiest to handle.

Br.
Michael
Split let url = *** into ServIP, Port, xmlcommand
 
 
Q