In Swift, What's the Difference Between Calling Uinavigationcontroller() VS Uinavigationcontroller.Init()

Difference between UIView.init and without init in swift 3

Both are the same and call the same init function of UIView. It is best practice to not directly call the init though and just use UIView(frame:)

What is difference between create object with init and () in Swift

There is no functional difference between the two. Both styles will call the same initializer and produce the same value.


Most style guides that I've seen prefer to leave out the explicit .init-part in favor of the shorter A(value:) syntax — that also resembles the constructor syntax in many other languages.

That said, there are some scenarios where it's useful to be able to explicitly reference the initializer. For example:

  • when the type can be inferred and the act of initialization is more important than the type being initialized. Being able to call return .init(/* ... */) rather than return SomeComplicatedType(/* ... */) or let array: [SomeComplicatedType] = [.init(/* ... */), .init(/* ... */)]

  • when passing the initializer to a higher order function, being able to pass "something".map(String.init) rather than "something".map({ String($0) })

Again, it's a matter of style.

Initializing UINavigationController with Coordinator Pattern and Storyboard

I just tried this an it works. Are you doing something like this?

let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let nc = storyboard.instantiateInitialViewController() as? UINavigationController {
print("got the nav controller")
}
// or if it's not the initial, you have to set the id in the storyboard
if let nc = storyboard.instantiateViewController(withIdentifier: "Nav") as? UINavigationController {
print("got the nav controller")
}

What is difference between URL.init(string: ) and URL(string: )?

The first way you stated uses an Initializer.

Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters.

This means that they are used for:

  • To create an initial value.
  • To assign default property value within the property definition.
  • To initialize an instance for a particular data type 'init()' is used. No arguments are passed inside the init() function.

Therefore if you just want to use the data in the same instance you should use the first second method.

If you want to have the URL in anther instance and set it as a default value for your string you should use the first one.

But ultimately they will both do the same thing.

How to create custom UINavigationController class with custom init?

You can do the following :

class NavigationController: UINavigationController {
private let user: User

init(user: User, rootViewController: UIViewController, nibName nibNameOrNil: String? = nil, bundle nibBundleOrNil: Bundle? = nil) {
self.user = user
super.init(nibName: nil, bundle: nil)
self.viewControllers = [rootViewController]
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let vc = UIViewController()
let nc = NavigationController(user: User(), rootViewController: vc)

Swift 3 - override initializer for UINavigationController to set rootviewcontroller

The init(rootViewController:) is defined in UINavigationController, which is the super class of your NavigationController class. Therefore, you should use super instead of self to refer to it:

init() {
super.init(rootViewController: rvc)
self.rvc.delegate = self
}

Since you have one other initializer defined in NavigationController, Xcode thinks that you were trying to call that initializer. That's why it tells you to put coder: as the argument label.

Access Custom UINavigationController properties and methods in a UIViewController

You should cast it to your custom class type

if let navController = self.navigationController as? MyNavigationController {
navController.customMethod()
}


Related Topics



Leave a reply



Submit