In Swift, what's the difference between calling UINavigationController() vs UINavigationController.init()?

后端 未结 3 1919
时光取名叫无心
时光取名叫无心 2020-12-04 00:46

In Swift, what\'s the difference between calling UINavigation() vs UINavigation.init()? They both seem to return valid instance of UINavigati

相关标签:
3条回答
  • 2020-12-04 01:25

    UINavigationController() and UINavigationController.init() are the exact same thing. You can verify this by typing both into a Playground and then option-clicking on them. Both bring up the documentation for the same initializer.

    The Swift convention is to use just the type name (without .init).

    0 讨论(0)
  • 2020-12-04 01:26

    For some given type (e.g. UINavigationController), there is no difference between a call to UINavigationController() or UINavigationController.init(), but the latter syntax can (without the () call) be useful when referencing an initializer of some given type, say Foo, in contexts where we want to make use of a closure (or reference to a closure) which is to have

    • zero or more arguments, and
    • return type Foo,

    e.g., (Int, Double) -> Foo. In these contexts, using the syntax Foo.init may prove useful: rather than explicitly letting a closure repeatedly call a known initializer (piping the closure's arguments to the initializer), we may use (a reference to) the initializer direcly as the closure instead. If there's no ambiguity in the argument of the initializers of Foo, a reference to Foo.init in some given closure type context will resolve, using type inference, to the correct initializer.

    E.g., consider the following example

    struct Foo {
        let foo: Int
    
        // (Int) -> Foo
        init(foo: Int) {
            self.foo = 2*foo
        }
    
        // (Int, Int) -> Foo
        init(foo: Int, bar: Int) {
            self.foo = foo + bar
        }
    
        // () -> Foo
        init() {
            self.foo = 42
        }
    }
    
    let arr = [1, 2, 3]
    let fooArr1 = arr.map { Foo(foo: $0) }
    let fooArr2 = arr.map(Foo.init)
                     /* map operation expects a single argument of type (Int) -> Foo, 
                        which we generally supply as a trailing closure. In this context,
                        Swift can, without ambiguity (since we have none, is this example), 
                        find the correct overload among the initializers of Foo */
    print(fooArr1.map { $0.foo }, fooArr2.map { $0.foo }) // [2, 4, 6] [2, 4, 6]
    
    let emptyTupArr = [(), (), ()]
    let fooArr3 = emptyTupArr.map(Foo.init) // inferred to be the '() -> Foo' initializer
    print(fooArr3.map { $0.foo }) // [42, 42, 42]
    
    0 讨论(0)
  • 2020-12-04 01:51

    From Apple docs, you use init when you are subclassing the controller. It looks like without passing a value to the unit function, it just returns a standard UINavigationController

    https://developer.apple.com/reference/uikit/uinavigationcontroller

    0 讨论(0)
提交回复
热议问题