Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,897 questions

51,828 answers

573 users

How to parse URL in Swift

2 Answers

0 votes
import Foundation

if let url = URL(string: "https://collectivesolver.com:8080/path?query=item#fragment") {
    print("Scheme: \(url.scheme ?? "No scheme")")
    print("Host: \(url.host ?? "No host")")
    print("Port: \(url.port ?? 0)")
    print("Path: \(url.path)")
    print("Query: \(url.query ?? "No query")")
    print("Fragment: \(url.fragment ?? "No fragment")")
}



/*
run:

Scheme: https
Host: collectivesolver.com
Port: 8080
Path: /path
Query: query=item
Fragment: fragment

*/

 



answered Feb 1, 2025 by avibootz
0 votes
import Foundation

let urlStr = "https://seek4info.com:8080/path?query=item#fragment"

if let url = URL(string: urlStr),
   let components = URLComponents(url: url, resolvingAgainstBaseURL: false) {
    print("Scheme: \(components.scheme ?? "No scheme")")
    print("Host: \(components.host ?? "No host")")
    print("Port: \(components.port ?? 0)")
    print("Path: \(components.path)")
    print("Query Items: \(components.queryItems ?? [])")
    print("Fragment: \(components.fragment ?? "No fragment")")
}



/*
run:

Scheme: https
Host: seek4info.com
Port: 8080
Path: /path
Query Items: [query=item]
Fragment: fragment

*/

 



answered Feb 1, 2025 by avibootz

Related questions

1 answer 111 views
111 views asked Feb 1, 2025 by avibootz
1 answer 76 views
76 views asked Feb 1, 2025 by avibootz
1 answer 105 views
105 views asked Feb 1, 2025 by avibootz
1 answer 75 views
1 answer 75 views
1 answer 43 views
43 views asked Jan 31, 2025 by avibootz
...