0

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)

2
  • 2
    URLRequest(url: APIEndpoint.login) should work. The issue is that APIEndpoint.shared is a specific APIEndpoint instance, like you do: let specificOne = APIEndpoint.shared, it can't use a static` method then. Commented Nov 25, 2019 at 8:48
  • @CôngToànLê Also. for future reference, if you want to reach something through your singleton, you don't need to make the other values inside your singleton static as well. It's enough with having the shared static to reach the other values Commented Nov 25, 2019 at 8:57

1 Answer 1

1

login and baseURL are static members of class APIEndpoint. You're already could use them as APIEndpoint.login and APIEndpoint.baseURL. In your case you should write

var request = URLRequest(url: APIEndpoint.login)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.