Home > Software engineering >  UINavigationController in programmatically
UINavigationController in programmatically

Time:02-22

can someone help me pls ? I trying to start my app buy i have error in AppDelegate. So in my AppDelegate I created navigationController and xcode wants that i use init from TableVC but i cant. How to fix it ? Thanks

Here is the compile error I get

Here is my TableVC init

My code:

AppDelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let viewController = TableVC(container: TableVC.Depencies) <- and there i have error "Editor placeholder in source file" , i guess xcode wants init from tableVC 
        self.naviController = UINavigationController(rootViewController: viewController)
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = self.naviController
        
        self.window?.backgroundColor = UIColor.white
        self.window?.makeKeyAndVisible()
        
        return true
    }

TableVC:

typealias Depencies = ProtocolCoreData & APIService
private let jsonTask: APIService
private var cityTemps: [WeatherModel] = []
private let coreDataManaer: ProtocolCoreData



init(container: Depencies) {
    self.jsonTask = container.apiService as APIService
    self.coreDataManaer = container.coreDataService as ProtocolCoreData
    super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

CodePudding user response:

You are passing the type "TableVC.Depencies" and not an instance of it.

 let dependencies: TableVC.Depencies = .init()

... and pass it to the viewController

 let vc = TableVC(container: dependencies)

CodePudding user response:

Your init requires a type that conforms to ProtocolCoreData & APIService which I believe are protocols ?

You can try the following options:

1 - You can create an empty init if you do not want to pass the dependency on init.

// I removed your extra stuff just for example
class TableVC: UITableViewController
{
    typealias Dependancies = ProtocolCoreData & APIService
    
    // One option is to create a default init if you do not
    // want to provide depend
    init()
    {
        //do what you want
        super.init(nibName: nil, bundle: nil)
    }
    
    init(container: Dependancies)
    {
        // do what you want with container
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Then you need some kind of type which matches your TableVC.Dependancy profile, for example

class SomeClass: NSObject, ProtocolCoreData & APIService
{
    
}

You need to then initialize this type and pass this to your TableVC initializer

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool
{
    let dependency = SomeClass()
    
    let viewController = TableVC(container: dependency)
    
    // or let viewController = TableVC() if you don't want to set dependancy
    
    self.naviController = UINavigationController(rootViewController: viewController)
    
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = self.naviController
    
    self.window?.backgroundColor = UIColor.white
    self.window?.makeKeyAndVisible()
    
    return true
}

This should remove the compilation error

  • Related