Saturday, May 11, 2024
HomeiOS Developmentios - Swift: appendingPathComponent doesn't work after unwrap base Url

ios – Swift: appendingPathComponent doesn’t work after unwrap base Url


I carried out some capabilities to get Url, however I can’t do get accomplished url after unwrap the bottom url. I’ve these two variable as follows:

var baseUrl: URL? {
        change self{
        case .getNews:
            if let url = URL(string: "https://api.lil.software program"){
                return url
            }else{
                return nil
            }
        }
    }
    
var path: String? {
    return "/information"
}

I wish to get the finished Url, so I carried out a perform as follows

var urlRequest: URLRequest? {
        if var url = self.baseUrl, var path = self.path{
            return URLRequest(url: url.appendingPathExtension(path))
        }else{
            return nil
        }
    }

After I name endpoint.urlRequest! I believe I ought to get “https://api.lil.software program/information”, however I obtained “https://api.lil.software program”

Nonetheless, if I carried out var urlRequest: URLRequest? with out unwrapping base url as follows, I can get the finished url like “https://api.lil.software program/information”

var urlRequest: URLRequest? {
        return URLRequest(url: self.baseUrl!.appendingPathComponent(self.path!))
    }

Why there’s completely different? due to if var? thanks to your assist.

I attempted to make use of guard let/var and many others, however I’ve the identical outcome. I used the unsuitable approach? thanks to your response.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments