Currently, I want to write a APIEndpoint class with contains all of my APIs. I have a variable baseURL and I want to use it for the remains. But when I tried to write it as below, it comes to error.
class APIEndpoint {
static let shared = APIEndpoint()
static let baseURL = "https://13.251.102.94:5001/api/"
static let login = URL(string: baseURL + "auth/customer/login")!
}
My function
func performLogin () {
var request = URLRequest(url: APIEndpoint.shared.login)
request.httpMethod = "POST"
let json: [String: String] = ["username": self.name, "password": self.password]
request.httpBody = try? JSONSerialization.data(withJSONObject: json)
Network.shared.session.dataTask(with: request) { (data, response, err) in
guard let data = data else { return }
print(JSON(data)["data"])
}.resume()
}
Error:
Static member 'login' cannot be used on instance of type 'APIEndpoint'
in line: var request = URLRequest(url: APIEndpoint.shared.login)
URLRequest(url: APIEndpoint.login)should work. The issue is thatAPIEndpoint.sharedis a specificAPIEndpointinstance, like you do: let specificOne = APIEndpoint.shared, it can't use astatic` method then.sharedstatic to reach the other values