0

I have a network manager struct. I'm trying to initialize it in my main UIViewController but it is failing.

struct NetworkManager {
    static var enviroment: NetworkEnviroment = .production
    static var apiKey = ""
    private let mainApiRouter = Router<MainApi>()

    func login(username: String, pass: String, completion:@escaping(_ product:User?, _ err: String?) ->()){
        mainApiRouter.request(.login(username: username, password: pass)) { (data, resp, err) in
            if err != nil {
                completion(nil, "Please check your network connection")
            } else {
                if let reponse = resp as? HTTPURLResponse {
                    let result = handelNetworkResponse(reponse)
                    switch result {
                    case .success:
                        guard let responseData = data else {
                            completion(nil, NetworkResponse.noData.rawValue)
                            return
                        }
                        do {
                            let apiResponse = try JSONDecoder().decode(UserData.self, from: responseData)
                            if apiResponse.data != nil {
                                completion(apiResponse.data, nil)
                            } else {
                                completion(nil, "User login failed")
                            }
                        }catch(let error) {
                            completion(nil, "\(error.localizedDescription)")
                        }
                    case .failure(let failureError):
                        completion(nil, failureError)
                    }
                }
            }
        }
    }
}

Then this is how I'm initializing it in the view controller so I would be able to use the login function within the network manager struct. And when I call the super.init coder the network manager is nil.

class LoginViewController: UIViewController {
    var networkManager: NetworkManager!

    init(networkManager: NetworkManager) {
        super.init(nibName: nil, bundle: nil)
        self.networkManager = networkManager
    }

    required init?(coder aDecoder: NSCoder) {
        // super.init(coder: aDecoder)
        fatalError("init(coder:) has not been implemented")
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kegham K.
  • 1,589
  • 22
  • 40
  • You should explain what the error is... – l'L'l Sep 30 '18 at 22:03
  • The error that it is not being initialized. fatalError – Kegham K. Sep 30 '18 at 22:04
  • How are you creating the instance of `LoginViewController`? And which line exactly is causing the error? – rmaddy Sep 30 '18 at 22:06
  • @rmaddy It is just a view controller that i have created in the storyboard gave it a custom class. There error is in the fatalError. "init(coder:)has not been implemented" – Kegham K. Sep 30 '18 at 22:09
  • You are using a storyboard. You need remove the call to `fatalError` and actually implement the initializer. – rmaddy Sep 30 '18 at 22:14
  • @rmaddy i removed the fatalError and called `super.init(coder: aDecoder)` but when i try to call the function within the struct the network manager is still nil. Is this the right way to do it? – Kegham K. Sep 30 '18 at 22:17

1 Answers1

0

Your issue is that you are trying to load this view controller from the storyboard, but you haven't implemented the init(coder:) that the storyboard uses to initialise views created in the storyboard. If you remove the fatalError and use super.init(coder: aDecoder) then your view controller will load successfully, but the code that you are using to initialise your network manager will never be called because the storyboard doesn't call that initialiser when loading your view controller - you will have to move that code somewhere else.

William Taylor
  • 691
  • 8
  • 23